Lines Matching full:sensor

99 static int stm_enable_irq(struct stm_thermal_sensor *sensor)  in stm_enable_irq()  argument
103 dev_dbg(sensor->dev, "low:%d high:%d\n", sensor->low_temp_enabled, in stm_enable_irq()
104 sensor->high_temp_enabled); in stm_enable_irq()
107 value = readl_relaxed(sensor->base + DTS_ITENR_OFFSET); in stm_enable_irq()
110 if (sensor->low_temp_enabled) in stm_enable_irq()
113 if (sensor->high_temp_enabled) in stm_enable_irq()
117 writel_relaxed(value, sensor->base + DTS_ITENR_OFFSET); in stm_enable_irq()
124 struct stm_thermal_sensor *sensor = sdata; in stm_thermal_irq_handler() local
126 dev_dbg(sensor->dev, "sr:%d\n", in stm_thermal_irq_handler()
127 readl_relaxed(sensor->base + DTS_SR_OFFSET)); in stm_thermal_irq_handler()
129 thermal_zone_device_update(sensor->th_dev, THERMAL_EVENT_UNSPECIFIED); in stm_thermal_irq_handler()
131 stm_enable_irq(sensor); in stm_thermal_irq_handler()
134 writel_relaxed(ICIFR_MASK, sensor->base + DTS_ICIFR_OFFSET); in stm_thermal_irq_handler()
139 static int stm_sensor_power_on(struct stm_thermal_sensor *sensor) in stm_sensor_power_on() argument
144 /* Enable sensor */ in stm_sensor_power_on()
145 value = readl_relaxed(sensor->base + DTS_CFGR1_OFFSET); in stm_sensor_power_on()
147 writel_relaxed(value, sensor->base + DTS_CFGR1_OFFSET); in stm_sensor_power_on()
154 ret = readl_poll_timeout(sensor->base + DTS_SR_OFFSET, in stm_sensor_power_on()
161 value = readl_relaxed(sensor->base + in stm_sensor_power_on()
164 writel_relaxed(value, sensor->base + in stm_sensor_power_on()
167 sensor->mode = THERMAL_DEVICE_ENABLED; in stm_sensor_power_on()
172 static int stm_sensor_power_off(struct stm_thermal_sensor *sensor) in stm_sensor_power_off() argument
176 sensor->mode = THERMAL_DEVICE_DISABLED; in stm_sensor_power_off()
179 value = readl_relaxed(sensor->base + DTS_CFGR1_OFFSET); in stm_sensor_power_off()
181 writel_relaxed(value, sensor->base + DTS_CFGR1_OFFSET); in stm_sensor_power_off()
186 /* Disable sensor */ in stm_sensor_power_off()
187 value = readl_relaxed(sensor->base + DTS_CFGR1_OFFSET); in stm_sensor_power_off()
189 writel_relaxed(value, sensor->base + DTS_CFGR1_OFFSET); in stm_sensor_power_off()
192 return readl_poll_timeout(sensor->base + DTS_SR_OFFSET, value, in stm_sensor_power_off()
197 static int stm_thermal_calibration(struct stm_thermal_sensor *sensor) in stm_thermal_calibration() argument
203 clk_freq = clk_get_rate(sensor->clk); in stm_thermal_calibration()
214 value = readl_relaxed(sensor->base + DTS_CFGR1_OFFSET); in stm_thermal_calibration()
235 writel_relaxed(value, sensor->base + DTS_CFGR1_OFFSET); in stm_thermal_calibration()
240 /* Fill in DTS structure with factory sensor values */
241 static int stm_thermal_read_factory_settings(struct stm_thermal_sensor *sensor) in stm_thermal_read_factory_settings() argument
244 sensor->t0 = readl_relaxed(sensor->base + DTS_T0VALR1_OFFSET) & in stm_thermal_read_factory_settings()
246 if (!sensor->t0) in stm_thermal_read_factory_settings()
247 sensor->t0 = TS1_T0_VAL0; in stm_thermal_read_factory_settings()
249 sensor->t0 = TS1_T0_VAL1; in stm_thermal_read_factory_settings()
252 sensor->fmt0 = ADJUST * (readl_relaxed(sensor->base + in stm_thermal_read_factory_settings()
256 sensor->ramp_coeff = readl_relaxed(sensor->base + DTS_RAMPVALR_OFFSET) & in stm_thermal_read_factory_settings()
259 if (!sensor->fmt0 || !sensor->ramp_coeff) { in stm_thermal_read_factory_settings()
260 dev_err(sensor->dev, "%s: wrong setting\n", __func__); in stm_thermal_read_factory_settings()
264 dev_dbg(sensor->dev, "%s: T0 = %doC, FMT0 = %dHz, RAMP_COEFF = %dHz/oC", in stm_thermal_read_factory_settings()
265 __func__, sensor->t0, sensor->fmt0, sensor->ramp_coeff); in stm_thermal_read_factory_settings()
270 static int stm_thermal_calculate_threshold(struct stm_thermal_sensor *sensor, in stm_thermal_calculate_threshold() argument
276 freqM = ((temp - sensor->t0) * sensor->ramp_coeff) / 1000 + in stm_thermal_calculate_threshold()
277 sensor->fmt0; in stm_thermal_calculate_threshold()
280 *th = clk_get_rate(sensor->clk) * SAMPLING_TIME / freqM; in stm_thermal_calculate_threshold()
284 dev_dbg(sensor->dev, "freqM=%d Hz, threshold=0x%x", freqM, *th); in stm_thermal_calculate_threshold()
290 static int stm_disable_irq(struct stm_thermal_sensor *sensor) in stm_disable_irq() argument
295 value = readl_relaxed(sensor->base + DTS_ITENR_OFFSET); in stm_disable_irq()
297 writel_relaxed(value, sensor->base + DTS_ITENR_OFFSET); in stm_disable_irq()
304 struct stm_thermal_sensor *sensor = thermal_zone_device_priv(tz); in stm_thermal_set_trips() local
308 dev_dbg(sensor->dev, "set trips %d <--> %d\n", low, high); in stm_thermal_set_trips()
311 itr1 = readl_relaxed(sensor->base + DTS_ITR1_OFFSET); in stm_thermal_set_trips()
320 sensor->low_temp_enabled = 1; in stm_thermal_set_trips()
322 ret = stm_thermal_calculate_threshold(sensor, low - 500, &th); in stm_thermal_set_trips()
328 sensor->low_temp_enabled = 0; in stm_thermal_set_trips()
333 sensor->high_temp_enabled = 1; in stm_thermal_set_trips()
334 ret = stm_thermal_calculate_threshold(sensor, high, &th); in stm_thermal_set_trips()
340 sensor->high_temp_enabled = 0; in stm_thermal_set_trips()
344 writel_relaxed(itr1, sensor->base + DTS_ITR1_OFFSET); in stm_thermal_set_trips()
352 struct stm_thermal_sensor *sensor = thermal_zone_device_priv(tz); in stm_thermal_get_temp() local
356 if (sensor->mode != THERMAL_DEVICE_ENABLED) in stm_thermal_get_temp()
360 ret = readl_relaxed_poll_timeout(sensor->base + DTS_DR_OFFSET, periods, in stm_thermal_get_temp()
367 freqM = (clk_get_rate(sensor->clk) * SAMPLING_TIME) / periods; in stm_thermal_get_temp()
372 *temp = (freqM - sensor->fmt0) * 1000 / sensor->ramp_coeff + sensor->t0; in stm_thermal_get_temp()
378 static int stm_register_irq(struct stm_thermal_sensor *sensor) in stm_register_irq() argument
380 struct device *dev = sensor->dev; in stm_register_irq()
384 sensor->irq = platform_get_irq(pdev, 0); in stm_register_irq()
385 if (sensor->irq < 0) in stm_register_irq()
386 return sensor->irq; in stm_register_irq()
388 ret = devm_request_threaded_irq(dev, sensor->irq, in stm_register_irq()
392 dev->driver->name, sensor); in stm_register_irq()
395 sensor->irq); in stm_register_irq()
404 static int stm_thermal_sensor_off(struct stm_thermal_sensor *sensor) in stm_thermal_sensor_off() argument
408 stm_disable_irq(sensor); in stm_thermal_sensor_off()
410 ret = stm_sensor_power_off(sensor); in stm_thermal_sensor_off()
414 clk_disable_unprepare(sensor->clk); in stm_thermal_sensor_off()
419 static int stm_thermal_prepare(struct stm_thermal_sensor *sensor) in stm_thermal_prepare() argument
423 ret = clk_prepare_enable(sensor->clk); in stm_thermal_prepare()
427 ret = stm_thermal_read_factory_settings(sensor); in stm_thermal_prepare()
431 ret = stm_thermal_calibration(sensor); in stm_thermal_prepare()
438 clk_disable_unprepare(sensor->clk); in stm_thermal_prepare()
445 struct stm_thermal_sensor *sensor = dev_get_drvdata(dev); in stm_thermal_suspend() local
447 return stm_thermal_sensor_off(sensor); in stm_thermal_suspend()
453 struct stm_thermal_sensor *sensor = dev_get_drvdata(dev); in stm_thermal_resume() local
455 ret = stm_thermal_prepare(sensor); in stm_thermal_resume()
459 ret = stm_sensor_power_on(sensor); in stm_thermal_resume()
463 thermal_zone_device_update(sensor->th_dev, THERMAL_EVENT_UNSPECIFIED); in stm_thermal_resume()
464 stm_enable_irq(sensor); in stm_thermal_resume()
485 struct stm_thermal_sensor *sensor; in stm_thermal_probe() local
495 sensor = devm_kzalloc(&pdev->dev, sizeof(*sensor), GFP_KERNEL); in stm_thermal_probe()
496 if (!sensor) in stm_thermal_probe()
499 platform_set_drvdata(pdev, sensor); in stm_thermal_probe()
501 sensor->dev = &pdev->dev; in stm_thermal_probe()
507 /* Populate sensor */ in stm_thermal_probe()
508 sensor->base = base; in stm_thermal_probe()
510 sensor->clk = devm_clk_get(&pdev->dev, "pclk"); in stm_thermal_probe()
511 if (IS_ERR(sensor->clk)) { in stm_thermal_probe()
514 return PTR_ERR(sensor->clk); in stm_thermal_probe()
517 stm_disable_irq(sensor); in stm_thermal_probe()
520 writel_relaxed(ICIFR_MASK, sensor->base + DTS_ICIFR_OFFSET); in stm_thermal_probe()
522 /* Configure and enable HW sensor */ in stm_thermal_probe()
523 ret = stm_thermal_prepare(sensor); in stm_thermal_probe()
525 dev_err(&pdev->dev, "Error prepare sensor: %d\n", ret); in stm_thermal_probe()
529 ret = stm_sensor_power_on(sensor); in stm_thermal_probe()
531 dev_err(&pdev->dev, "Error power on sensor: %d\n", ret); in stm_thermal_probe()
535 sensor->th_dev = devm_thermal_of_zone_register(&pdev->dev, 0, in stm_thermal_probe()
536 sensor, in stm_thermal_probe()
539 if (IS_ERR(sensor->th_dev)) { in stm_thermal_probe()
540 dev_err(&pdev->dev, "%s: thermal zone sensor registering KO\n", in stm_thermal_probe()
542 ret = PTR_ERR(sensor->th_dev); in stm_thermal_probe()
547 ret = stm_register_irq(sensor); in stm_thermal_probe()
551 stm_enable_irq(sensor); in stm_thermal_probe()
557 ret = thermal_add_hwmon_sysfs(sensor->th_dev); in stm_thermal_probe()
572 struct stm_thermal_sensor *sensor = platform_get_drvdata(pdev); in stm_thermal_remove() local
574 stm_thermal_sensor_off(sensor); in stm_thermal_remove()
575 thermal_remove_hwmon_sysfs(sensor->th_dev); in stm_thermal_remove()
589 MODULE_DESCRIPTION("STMicroelectronics STM32 Thermal Sensor Driver");