1// SPDX-License-Identifier: GPL-2.0-only 2/* 3 * Copyright (C) ST-Ericsson 2010 - 2013 4 * Author: Martin Persson <martin.persson@stericsson.com> 5 * Hongbo Zhang <hongbo.zhang@linaro.org> 6 * 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. 10 * 11 * When AB8500 thermal shutdown temperature is reached a hardware shutdown of 12 * the AB8500 will occur. 13 */ 14 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> 27#include "abx500.h" 28 29#define DEFAULT_POWER_OFF_DELAY (HZ * 10) 30#define THERMAL_VCC 1800 31#define PULL_UP_RESISTOR 47000 32 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 38 39struct ab8500_gpadc_cfg { 40 const struct abx500_res_to_temp *temp_tbl; 41 int tbl_sz; 42 int vcc; 43 int r_up; 44}; 45 46struct ab8500_temp { 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; 53}; 54 55/* 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. 60 */ 61static int ab8500_voltage_to_temp(struct ab8500_gpadc_cfg *cfg, 62 int v_ntc, int *temp) 63{ 64 int r_ntc, i = 0, tbl_sz = cfg->tbl_sz; 65 const struct abx500_res_to_temp *tbl = cfg->temp_tbl; 66 67 if (cfg->vcc < 0 || v_ntc >= cfg->vcc) 68 return -EINVAL; 69 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) 72 return -EINVAL; 73 74 while (!(r_ntc <= tbl[i].resist && r_ntc > tbl[i + 1].resist) && 75 i < tbl_sz - 2) 76 i++; 77 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); 81 82 return 0; 83} 84 85static int ab8500_read_sensor(struct abx500_temp *data, u8 sensor, int *temp) 86{ 87 int voltage, ret; 88 struct ab8500_temp *ab8500_data = data->plat_data; 89 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); 96 if (ret < 0) 97 return ret; 98 ret = ab8500_voltage_to_temp(&ab8500_data->cfg, voltage, temp); 99 if (ret < 0) 100 return ret; 101 } else if (sensor == AB8500_SENSOR_AUX2) { 102 ret = iio_read_channel_processed(ab8500_data->aux2, &voltage); 103 if (ret < 0) 104 return ret; 105 ret = ab8500_voltage_to_temp(&ab8500_data->cfg, voltage, temp); 106 if (ret < 0) 107 return ret; 108 } 109 110 return 0; 111} 112 113static void ab8500_thermal_power_off(struct work_struct *work) 114{ 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; 118 119 dev_warn(&abx500_data->pdev->dev, "Power off due to critical temp\n"); 120 121 kernel_power_off(); 122} 123 124static ssize_t ab8500_show_name(struct device *dev, 125 struct device_attribute *devattr, char *buf) 126{ 127 return sprintf(buf, "ab8500\n"); 128} 129 130static ssize_t ab8500_show_label(struct device *dev, 131 struct device_attribute *devattr, char *buf) 132{ 133 char *label; 134 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 135 int index = attr->index; 136 137 switch (index) { 138 case 1: 139 label = "ext_adc1"; 140 break; 141 case 2: 142 label = "ext_adc2"; 143 break; 144 case 3: 145 label = "bat_temp"; 146 break; 147 case 4: 148 label = "bat_ctrl"; 149 break; 150 default: 151 return -EINVAL; 152 } 153 154 return sprintf(buf, "%s\n", label); 155} 156 157static int ab8500_temp_irq_handler(int irq, struct abx500_temp *data) 158{ 159 struct ab8500_temp *ab8500_data = data->plat_data; 160 161 dev_warn(&data->pdev->dev, "Power off in %d s\n", 162 DEFAULT_POWER_OFF_DELAY / HZ); 163 164 schedule_delayed_work(&ab8500_data->power_off_work, 165 DEFAULT_POWER_OFF_DELAY); 166 return 0; 167} 168 169int abx500_hwmon_init(struct abx500_temp *data) 170{ 171 struct ab8500_temp *ab8500_data; 172 173 ab8500_data = devm_kzalloc(&data->pdev->dev, sizeof(*ab8500_data), 174 GFP_KERNEL); 175 if (!ab8500_data) 176 return -ENOMEM; 177 178 ab8500_data->btemp = ab8500_btemp_get(); 179 if (IS_ERR(ab8500_data->btemp)) 180 return PTR_ERR(ab8500_data->btemp); 181 182 INIT_DELAYED_WORK(&ab8500_data->power_off_work, 183 ab8500_thermal_power_off); 184 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; 189 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); 197 } 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); 204 } 205 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; 211 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; 217 218 return 0; 219} 220EXPORT_SYMBOL(abx500_hwmon_init); 221 222MODULE_AUTHOR("Hongbo Zhang <hongbo.zhang@linaro.org>"); 223MODULE_DESCRIPTION("AB8500 temperature driver"); 224MODULE_LICENSE("GPL"); 225