1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) ST-Ericsson 2010 - 2013
7 * When the AB8500 thermal warning temperature is reached (threshold cannot
8 * be changed by SW), an interrupt is set, and if no further action is taken
9 * within a certain time frame, kernel_power_off will be called.
11 * When AB8500 thermal shutdown temperature is reached a hardware shutdown of
12 * the AB8500 will occur.
15 #include <linux/err.h>
16 #include <linux/hwmon.h>
17 #include <linux/hwmon-sysfs.h>
18 #include <linux/mfd/abx500.h>
19 #include <linux/mfd/abx500/ab8500-bm.h>
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/power/ab8500.h>
23 #include <linux/reboot.h>
24 #include <linux/slab.h>
25 #include <linux/sysfs.h>
26 #include <linux/iio/consumer.h>
29 #define DEFAULT_POWER_OFF_DELAY (HZ * 10)
30 #define THERMAL_VCC 1800
31 #define PULL_UP_RESISTOR 47000
33 #define AB8500_SENSOR_AUX1 0
34 #define AB8500_SENSOR_AUX2 1
35 #define AB8500_SENSOR_BTEMP_BALL 2
36 #define AB8500_SENSOR_BAT_CTRL 3
37 #define NUM_MONITORED_SENSORS 4
39 struct ab8500_gpadc_cfg {
40 const struct abx500_res_to_temp *temp_tbl;
47 struct iio_channel *aux1;
48 struct iio_channel *aux2;
49 struct ab8500_btemp *btemp;
50 struct delayed_work power_off_work;
51 struct ab8500_gpadc_cfg cfg;
52 struct abx500_temp *abx500_data;
56 * The hardware connection is like this:
57 * VCC----[ R_up ]-----[ NTC ]----GND
58 * where R_up is pull-up resistance, and GPADC measures voltage on NTC.
59 * and res_to_temp table is strictly sorted by falling resistance values.
61 static int ab8500_voltage_to_temp(struct ab8500_gpadc_cfg *cfg,
64 int r_ntc, i = 0, tbl_sz = cfg->tbl_sz;
65 const struct abx500_res_to_temp *tbl = cfg->temp_tbl;
67 if (cfg->vcc < 0 || v_ntc >= cfg->vcc)
70 r_ntc = v_ntc * cfg->r_up / (cfg->vcc - v_ntc);
71 if (r_ntc > tbl[0].resist || r_ntc < tbl[tbl_sz - 1].resist)
74 while (!(r_ntc <= tbl[i].resist && r_ntc > tbl[i + 1].resist) &&
78 /* return milli-Celsius */
79 *temp = tbl[i].temp * 1000 + ((tbl[i + 1].temp - tbl[i].temp) * 1000 *
80 (r_ntc - tbl[i].resist)) / (tbl[i + 1].resist - tbl[i].resist);
85 static int ab8500_read_sensor(struct abx500_temp *data, u8 sensor, int *temp)
88 struct ab8500_temp *ab8500_data = data->plat_data;
90 if (sensor == AB8500_SENSOR_BTEMP_BALL) {
91 *temp = ab8500_btemp_get_temp(ab8500_data->btemp);
92 } else if (sensor == AB8500_SENSOR_BAT_CTRL) {
93 *temp = ab8500_btemp_get_batctrl_temp(ab8500_data->btemp);
94 } else if (sensor == AB8500_SENSOR_AUX1) {
95 ret = iio_read_channel_processed(ab8500_data->aux1, &voltage);
98 ret = ab8500_voltage_to_temp(&ab8500_data->cfg, voltage, temp);
101 } else if (sensor == AB8500_SENSOR_AUX2) {
102 ret = iio_read_channel_processed(ab8500_data->aux2, &voltage);
105 ret = ab8500_voltage_to_temp(&ab8500_data->cfg, voltage, temp);
113 static void ab8500_thermal_power_off(struct work_struct *work)
115 struct ab8500_temp *ab8500_data = container_of(work,
116 struct ab8500_temp, power_off_work.work);
117 struct abx500_temp *abx500_data = ab8500_data->abx500_data;
119 dev_warn(&abx500_data->pdev->dev, "Power off due to critical temp\n");
124 static ssize_t ab8500_show_name(struct device *dev,
125 struct device_attribute *devattr, char *buf)
127 return sprintf(buf, "ab8500\n");
130 static ssize_t ab8500_show_label(struct device *dev,
131 struct device_attribute *devattr, char *buf)
134 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
135 int index = attr->index;
154 return sprintf(buf, "%s\n", label);
157 static int ab8500_temp_irq_handler(int irq, struct abx500_temp *data)
159 struct ab8500_temp *ab8500_data = data->plat_data;
161 dev_warn(&data->pdev->dev, "Power off in %d s\n",
162 DEFAULT_POWER_OFF_DELAY / HZ);
164 schedule_delayed_work(&ab8500_data->power_off_work,
165 DEFAULT_POWER_OFF_DELAY);
169 int abx500_hwmon_init(struct abx500_temp *data)
171 struct ab8500_temp *ab8500_data;
173 ab8500_data = devm_kzalloc(&data->pdev->dev, sizeof(*ab8500_data),
178 ab8500_data->btemp = ab8500_btemp_get();
179 if (IS_ERR(ab8500_data->btemp))
180 return PTR_ERR(ab8500_data->btemp);
182 INIT_DELAYED_WORK(&ab8500_data->power_off_work,
183 ab8500_thermal_power_off);
185 ab8500_data->cfg.vcc = THERMAL_VCC;
186 ab8500_data->cfg.r_up = PULL_UP_RESISTOR;
187 ab8500_data->cfg.temp_tbl = ab8500_temp_tbl_a_thermistor;
188 ab8500_data->cfg.tbl_sz = ab8500_temp_tbl_a_size;
190 data->plat_data = ab8500_data;
191 ab8500_data->aux1 = devm_iio_channel_get(&data->pdev->dev, "aux1");
192 if (IS_ERR(ab8500_data->aux1)) {
193 if (PTR_ERR(ab8500_data->aux1) == -ENODEV)
194 return -EPROBE_DEFER;
195 dev_err(&data->pdev->dev, "failed to get AUX1 ADC channel\n");
196 return PTR_ERR(ab8500_data->aux1);
198 ab8500_data->aux2 = devm_iio_channel_get(&data->pdev->dev, "aux2");
199 if (IS_ERR(ab8500_data->aux2)) {
200 if (PTR_ERR(ab8500_data->aux2) == -ENODEV)
201 return -EPROBE_DEFER;
202 dev_err(&data->pdev->dev, "failed to get AUX2 ADC channel\n");
203 return PTR_ERR(ab8500_data->aux2);
206 data->gpadc_addr[0] = AB8500_SENSOR_AUX1;
207 data->gpadc_addr[1] = AB8500_SENSOR_AUX2;
208 data->gpadc_addr[2] = AB8500_SENSOR_BTEMP_BALL;
209 data->gpadc_addr[3] = AB8500_SENSOR_BAT_CTRL;
210 data->monitored_sensors = NUM_MONITORED_SENSORS;
212 data->ops.read_sensor = ab8500_read_sensor;
213 data->ops.irq_handler = ab8500_temp_irq_handler;
214 data->ops.show_name = ab8500_show_name;
215 data->ops.show_label = ab8500_show_label;
216 data->ops.is_visible = NULL;
220 EXPORT_SYMBOL(abx500_hwmon_init);
223 MODULE_DESCRIPTION("AB8500 temperature driver");
224 MODULE_LICENSE("GPL");