Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 1 | /* |
| 2 | * HWMON Driver for Dialog DA9052 |
| 3 | * |
| 4 | * Copyright(c) 2012 Dialog Semiconductor Ltd. |
| 5 | * |
| 6 | * Author: David Dajun Chen <dchen@diasemi.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the |
| 10 | * Free Software Foundation; either version 2 of the License, or (at your |
| 11 | * option) any later version. |
| 12 | * |
| 13 | */ |
| 14 | |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 15 | #include <linux/err.h> |
| 16 | #include <linux/hwmon.h> |
| 17 | #include <linux/hwmon-sysfs.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/platform_device.h> |
Sebastian Reichel | 4f16cab | 2017-08-21 16:54:04 +0200 | [diff] [blame] | 23 | #include <linux/property.h> |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 24 | |
| 25 | #include <linux/mfd/da9052/da9052.h> |
| 26 | #include <linux/mfd/da9052/reg.h> |
Sebastian Reichel | 4f16cab | 2017-08-21 16:54:04 +0200 | [diff] [blame] | 27 | #include <linux/regulator/consumer.h> |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 28 | |
| 29 | struct da9052_hwmon { |
Sebastian Reichel | 4f16cab | 2017-08-21 16:54:04 +0200 | [diff] [blame] | 30 | struct da9052 *da9052; |
| 31 | struct mutex hwmon_lock; |
| 32 | bool tsi_as_adc; |
| 33 | int tsiref_mv; |
| 34 | struct regulator *tsiref; |
| 35 | struct completion tsidone; |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | static const char * const input_names[] = { |
| 39 | [DA9052_ADC_VDDOUT] = "VDDOUT", |
| 40 | [DA9052_ADC_ICH] = "CHARGING CURRENT", |
| 41 | [DA9052_ADC_TBAT] = "BATTERY TEMP", |
| 42 | [DA9052_ADC_VBAT] = "BATTERY VOLTAGE", |
| 43 | [DA9052_ADC_IN4] = "ADC IN4", |
| 44 | [DA9052_ADC_IN5] = "ADC IN5", |
| 45 | [DA9052_ADC_IN6] = "ADC IN6", |
Sebastian Reichel | 4f16cab | 2017-08-21 16:54:04 +0200 | [diff] [blame] | 46 | [DA9052_ADC_TSI_XP] = "ADC TS X+", |
| 47 | [DA9052_ADC_TSI_YP] = "ADC TS Y+", |
| 48 | [DA9052_ADC_TSI_XN] = "ADC TS X-", |
| 49 | [DA9052_ADC_TSI_YN] = "ADC TS Y-", |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 50 | [DA9052_ADC_TJUNC] = "BATTERY JUNCTION TEMP", |
| 51 | [DA9052_ADC_VBBAT] = "BACK-UP BATTERY VOLTAGE", |
| 52 | }; |
| 53 | |
| 54 | /* Conversion function for VDDOUT and VBAT */ |
Guenter Roeck | 088ce2a | 2013-03-13 16:40:39 -0700 | [diff] [blame] | 55 | static inline int volt_reg_to_mv(int value) |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 56 | { |
Anthony Olech | c09088d | 2013-12-18 15:15:31 +0000 | [diff] [blame] | 57 | return DIV_ROUND_CLOSEST(value * 2000, 1023) + 2500; |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /* Conversion function for ADC channels 4, 5 and 6 */ |
Guenter Roeck | 088ce2a | 2013-03-13 16:40:39 -0700 | [diff] [blame] | 61 | static inline int input_reg_to_mv(int value) |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 62 | { |
| 63 | return DIV_ROUND_CLOSEST(value * 2500, 1023); |
| 64 | } |
| 65 | |
| 66 | /* Conversion function for VBBAT */ |
Guenter Roeck | 088ce2a | 2013-03-13 16:40:39 -0700 | [diff] [blame] | 67 | static inline int vbbat_reg_to_mv(int value) |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 68 | { |
Anthony Olech | c09088d | 2013-12-18 15:15:31 +0000 | [diff] [blame] | 69 | return DIV_ROUND_CLOSEST(value * 5000, 1023); |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 70 | } |
| 71 | |
Sebastian Reichel | 4f16cab | 2017-08-21 16:54:04 +0200 | [diff] [blame] | 72 | static inline int input_tsireg_to_mv(struct da9052_hwmon *hwmon, int value) |
| 73 | { |
| 74 | return DIV_ROUND_CLOSEST(value * hwmon->tsiref_mv, 1023); |
| 75 | } |
| 76 | |
Axel Lin | d9b24e37 | 2012-10-11 22:03:51 +0800 | [diff] [blame] | 77 | static inline int da9052_enable_vddout_channel(struct da9052 *da9052) |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 78 | { |
Axel Lin | d9b24e37 | 2012-10-11 22:03:51 +0800 | [diff] [blame] | 79 | return da9052_reg_update(da9052, DA9052_ADC_CONT_REG, |
| 80 | DA9052_ADCCONT_AUTOVDDEN, |
| 81 | DA9052_ADCCONT_AUTOVDDEN); |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 82 | } |
| 83 | |
Axel Lin | d9b24e37 | 2012-10-11 22:03:51 +0800 | [diff] [blame] | 84 | static inline int da9052_disable_vddout_channel(struct da9052 *da9052) |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 85 | { |
Axel Lin | d9b24e37 | 2012-10-11 22:03:51 +0800 | [diff] [blame] | 86 | return da9052_reg_update(da9052, DA9052_ADC_CONT_REG, |
| 87 | DA9052_ADCCONT_AUTOVDDEN, 0); |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 88 | } |
| 89 | |
Guenter Roeck | 6db587b | 2018-12-10 14:02:04 -0800 | [diff] [blame] | 90 | static ssize_t da9052_vddout_show(struct device *dev, |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 91 | struct device_attribute *devattr, char *buf) |
| 92 | { |
| 93 | struct da9052_hwmon *hwmon = dev_get_drvdata(dev); |
| 94 | int ret, vdd; |
| 95 | |
| 96 | mutex_lock(&hwmon->hwmon_lock); |
| 97 | |
| 98 | ret = da9052_enable_vddout_channel(hwmon->da9052); |
| 99 | if (ret < 0) |
| 100 | goto hwmon_err; |
| 101 | |
| 102 | vdd = da9052_reg_read(hwmon->da9052, DA9052_VDD_RES_REG); |
| 103 | if (vdd < 0) { |
| 104 | ret = vdd; |
| 105 | goto hwmon_err_release; |
| 106 | } |
| 107 | |
| 108 | ret = da9052_disable_vddout_channel(hwmon->da9052); |
| 109 | if (ret < 0) |
| 110 | goto hwmon_err; |
| 111 | |
| 112 | mutex_unlock(&hwmon->hwmon_lock); |
Guenter Roeck | 088ce2a | 2013-03-13 16:40:39 -0700 | [diff] [blame] | 113 | return sprintf(buf, "%d\n", volt_reg_to_mv(vdd)); |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 114 | |
| 115 | hwmon_err_release: |
| 116 | da9052_disable_vddout_channel(hwmon->da9052); |
| 117 | hwmon_err: |
| 118 | mutex_unlock(&hwmon->hwmon_lock); |
| 119 | return ret; |
| 120 | } |
| 121 | |
Guenter Roeck | 6db587b | 2018-12-10 14:02:04 -0800 | [diff] [blame] | 122 | static ssize_t da9052_ich_show(struct device *dev, |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 123 | struct device_attribute *devattr, char *buf) |
| 124 | { |
| 125 | struct da9052_hwmon *hwmon = dev_get_drvdata(dev); |
| 126 | int ret; |
| 127 | |
| 128 | ret = da9052_reg_read(hwmon->da9052, DA9052_ICHG_AV_REG); |
| 129 | if (ret < 0) |
| 130 | return ret; |
| 131 | |
| 132 | /* Equivalent to 3.9mA/bit in register ICHG_AV */ |
| 133 | return sprintf(buf, "%d\n", DIV_ROUND_CLOSEST(ret * 39, 10)); |
| 134 | } |
| 135 | |
Guenter Roeck | 6db587b | 2018-12-10 14:02:04 -0800 | [diff] [blame] | 136 | static ssize_t da9052_tbat_show(struct device *dev, |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 137 | struct device_attribute *devattr, char *buf) |
| 138 | { |
| 139 | struct da9052_hwmon *hwmon = dev_get_drvdata(dev); |
| 140 | |
| 141 | return sprintf(buf, "%d\n", da9052_adc_read_temp(hwmon->da9052)); |
| 142 | } |
| 143 | |
Guenter Roeck | 6db587b | 2018-12-10 14:02:04 -0800 | [diff] [blame] | 144 | static ssize_t da9052_vbat_show(struct device *dev, |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 145 | struct device_attribute *devattr, char *buf) |
| 146 | { |
| 147 | struct da9052_hwmon *hwmon = dev_get_drvdata(dev); |
| 148 | int ret; |
| 149 | |
| 150 | ret = da9052_adc_manual_read(hwmon->da9052, DA9052_ADC_VBAT); |
| 151 | if (ret < 0) |
| 152 | return ret; |
| 153 | |
Guenter Roeck | 088ce2a | 2013-03-13 16:40:39 -0700 | [diff] [blame] | 154 | return sprintf(buf, "%d\n", volt_reg_to_mv(ret)); |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 155 | } |
| 156 | |
Guenter Roeck | 6db587b | 2018-12-10 14:02:04 -0800 | [diff] [blame] | 157 | static ssize_t da9052_misc_channel_show(struct device *dev, |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 158 | struct device_attribute *devattr, |
| 159 | char *buf) |
| 160 | { |
| 161 | struct da9052_hwmon *hwmon = dev_get_drvdata(dev); |
| 162 | int channel = to_sensor_dev_attr(devattr)->index; |
| 163 | int ret; |
| 164 | |
| 165 | ret = da9052_adc_manual_read(hwmon->da9052, channel); |
| 166 | if (ret < 0) |
| 167 | return ret; |
| 168 | |
Guenter Roeck | 088ce2a | 2013-03-13 16:40:39 -0700 | [diff] [blame] | 169 | return sprintf(buf, "%d\n", input_reg_to_mv(ret)); |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 170 | } |
| 171 | |
Sebastian Reichel | 4f16cab | 2017-08-21 16:54:04 +0200 | [diff] [blame] | 172 | static int da9052_request_tsi_read(struct da9052_hwmon *hwmon, int channel) |
| 173 | { |
| 174 | u8 val = DA9052_TSICONTB_TSIMAN; |
| 175 | |
| 176 | switch (channel) { |
| 177 | case DA9052_ADC_TSI_XP: |
| 178 | val |= DA9052_TSICONTB_TSIMUX_XP; |
| 179 | break; |
| 180 | case DA9052_ADC_TSI_YP: |
| 181 | val |= DA9052_TSICONTB_TSIMUX_YP; |
| 182 | break; |
| 183 | case DA9052_ADC_TSI_XN: |
| 184 | val |= DA9052_TSICONTB_TSIMUX_XN; |
| 185 | break; |
| 186 | case DA9052_ADC_TSI_YN: |
| 187 | val |= DA9052_TSICONTB_TSIMUX_YN; |
| 188 | break; |
| 189 | } |
| 190 | |
| 191 | return da9052_reg_write(hwmon->da9052, DA9052_TSI_CONT_B_REG, val); |
| 192 | } |
| 193 | |
| 194 | static int da9052_get_tsi_result(struct da9052_hwmon *hwmon, int channel) |
| 195 | { |
| 196 | u8 regs[3]; |
| 197 | int msb, lsb, err; |
| 198 | |
| 199 | /* block read to avoid separation of MSB and LSB */ |
| 200 | err = da9052_group_read(hwmon->da9052, DA9052_TSI_X_MSB_REG, |
| 201 | ARRAY_SIZE(regs), regs); |
| 202 | if (err) |
| 203 | return err; |
| 204 | |
| 205 | switch (channel) { |
| 206 | case DA9052_ADC_TSI_XP: |
| 207 | case DA9052_ADC_TSI_XN: |
| 208 | msb = regs[0] << DA9052_TSILSB_TSIXL_BITS; |
| 209 | lsb = regs[2] & DA9052_TSILSB_TSIXL; |
| 210 | lsb >>= DA9052_TSILSB_TSIXL_SHIFT; |
| 211 | break; |
| 212 | case DA9052_ADC_TSI_YP: |
| 213 | case DA9052_ADC_TSI_YN: |
| 214 | msb = regs[1] << DA9052_TSILSB_TSIYL_BITS; |
| 215 | lsb = regs[2] & DA9052_TSILSB_TSIYL; |
| 216 | lsb >>= DA9052_TSILSB_TSIYL_SHIFT; |
| 217 | break; |
| 218 | default: |
| 219 | return -EINVAL; |
| 220 | } |
| 221 | |
| 222 | return msb | lsb; |
| 223 | } |
| 224 | |
| 225 | |
| 226 | static ssize_t __da9052_read_tsi(struct device *dev, int channel) |
| 227 | { |
| 228 | struct da9052_hwmon *hwmon = dev_get_drvdata(dev); |
| 229 | int ret; |
| 230 | |
| 231 | reinit_completion(&hwmon->tsidone); |
| 232 | |
| 233 | ret = da9052_request_tsi_read(hwmon, channel); |
| 234 | if (ret < 0) |
| 235 | return ret; |
| 236 | |
| 237 | /* Wait for an conversion done interrupt */ |
| 238 | if (!wait_for_completion_timeout(&hwmon->tsidone, |
| 239 | msecs_to_jiffies(500))) |
| 240 | return -ETIMEDOUT; |
| 241 | |
| 242 | return da9052_get_tsi_result(hwmon, channel); |
| 243 | } |
| 244 | |
Guenter Roeck | 6db587b | 2018-12-10 14:02:04 -0800 | [diff] [blame] | 245 | static ssize_t da9052_tsi_show(struct device *dev, |
| 246 | struct device_attribute *devattr, char *buf) |
Sebastian Reichel | 4f16cab | 2017-08-21 16:54:04 +0200 | [diff] [blame] | 247 | { |
| 248 | struct da9052_hwmon *hwmon = dev_get_drvdata(dev); |
| 249 | int channel = to_sensor_dev_attr(devattr)->index; |
| 250 | int ret; |
| 251 | |
| 252 | mutex_lock(&hwmon->hwmon_lock); |
| 253 | ret = __da9052_read_tsi(dev, channel); |
| 254 | mutex_unlock(&hwmon->hwmon_lock); |
| 255 | |
| 256 | if (ret < 0) |
| 257 | return ret; |
| 258 | else |
| 259 | return sprintf(buf, "%d\n", input_tsireg_to_mv(hwmon, ret)); |
| 260 | } |
| 261 | |
Guenter Roeck | 6db587b | 2018-12-10 14:02:04 -0800 | [diff] [blame] | 262 | static ssize_t da9052_tjunc_show(struct device *dev, |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 263 | struct device_attribute *devattr, char *buf) |
| 264 | { |
| 265 | struct da9052_hwmon *hwmon = dev_get_drvdata(dev); |
| 266 | int tjunc; |
| 267 | int toffset; |
| 268 | |
| 269 | tjunc = da9052_reg_read(hwmon->da9052, DA9052_TJUNC_RES_REG); |
| 270 | if (tjunc < 0) |
| 271 | return tjunc; |
| 272 | |
| 273 | toffset = da9052_reg_read(hwmon->da9052, DA9052_T_OFFSET_REG); |
| 274 | if (toffset < 0) |
| 275 | return toffset; |
| 276 | |
| 277 | /* |
| 278 | * Degrees celsius = 1.708 * (TJUNC_RES - T_OFFSET) - 108.8 |
| 279 | * T_OFFSET is a trim value used to improve accuracy of the result |
| 280 | */ |
| 281 | return sprintf(buf, "%d\n", 1708 * (tjunc - toffset) - 108800); |
| 282 | } |
| 283 | |
Guenter Roeck | 6db587b | 2018-12-10 14:02:04 -0800 | [diff] [blame] | 284 | static ssize_t da9052_vbbat_show(struct device *dev, |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 285 | struct device_attribute *devattr, char *buf) |
| 286 | { |
| 287 | struct da9052_hwmon *hwmon = dev_get_drvdata(dev); |
| 288 | int ret; |
| 289 | |
| 290 | ret = da9052_adc_manual_read(hwmon->da9052, DA9052_ADC_VBBAT); |
| 291 | if (ret < 0) |
| 292 | return ret; |
| 293 | |
Guenter Roeck | 088ce2a | 2013-03-13 16:40:39 -0700 | [diff] [blame] | 294 | return sprintf(buf, "%d\n", vbbat_reg_to_mv(ret)); |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 295 | } |
| 296 | |
Guenter Roeck | 6db587b | 2018-12-10 14:02:04 -0800 | [diff] [blame] | 297 | static ssize_t label_show(struct device *dev, |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 298 | struct device_attribute *devattr, char *buf) |
| 299 | { |
| 300 | return sprintf(buf, "%s\n", |
| 301 | input_names[to_sensor_dev_attr(devattr)->index]); |
| 302 | } |
| 303 | |
Sebastian Reichel | 4f16cab | 2017-08-21 16:54:04 +0200 | [diff] [blame] | 304 | static umode_t da9052_channel_is_visible(struct kobject *kobj, |
| 305 | struct attribute *attr, int index) |
| 306 | { |
| 307 | struct device *dev = container_of(kobj, struct device, kobj); |
| 308 | struct da9052_hwmon *hwmon = dev_get_drvdata(dev); |
| 309 | struct device_attribute *dattr = container_of(attr, |
| 310 | struct device_attribute, attr); |
| 311 | struct sensor_device_attribute *sattr = to_sensor_dev_attr(dattr); |
| 312 | |
| 313 | if (!hwmon->tsi_as_adc) { |
| 314 | switch (sattr->index) { |
| 315 | case DA9052_ADC_TSI_XP: |
| 316 | case DA9052_ADC_TSI_YP: |
| 317 | case DA9052_ADC_TSI_XN: |
| 318 | case DA9052_ADC_TSI_YN: |
| 319 | return 0; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | return attr->mode; |
| 324 | } |
| 325 | |
Guenter Roeck | 6db587b | 2018-12-10 14:02:04 -0800 | [diff] [blame] | 326 | static SENSOR_DEVICE_ATTR_RO(in0_input, da9052_vddout, DA9052_ADC_VDDOUT); |
| 327 | static SENSOR_DEVICE_ATTR_RO(in0_label, label, DA9052_ADC_VDDOUT); |
| 328 | static SENSOR_DEVICE_ATTR_RO(in3_input, da9052_vbat, DA9052_ADC_VBAT); |
| 329 | static SENSOR_DEVICE_ATTR_RO(in3_label, label, DA9052_ADC_VBAT); |
| 330 | static SENSOR_DEVICE_ATTR_RO(in4_input, da9052_misc_channel, DA9052_ADC_IN4); |
| 331 | static SENSOR_DEVICE_ATTR_RO(in4_label, label, DA9052_ADC_IN4); |
| 332 | static SENSOR_DEVICE_ATTR_RO(in5_input, da9052_misc_channel, DA9052_ADC_IN5); |
| 333 | static SENSOR_DEVICE_ATTR_RO(in5_label, label, DA9052_ADC_IN5); |
| 334 | static SENSOR_DEVICE_ATTR_RO(in6_input, da9052_misc_channel, DA9052_ADC_IN6); |
| 335 | static SENSOR_DEVICE_ATTR_RO(in6_label, label, DA9052_ADC_IN6); |
| 336 | static SENSOR_DEVICE_ATTR_RO(in9_input, da9052_vbbat, DA9052_ADC_VBBAT); |
| 337 | static SENSOR_DEVICE_ATTR_RO(in9_label, label, DA9052_ADC_VBBAT); |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 338 | |
Guenter Roeck | 6db587b | 2018-12-10 14:02:04 -0800 | [diff] [blame] | 339 | static SENSOR_DEVICE_ATTR_RO(in70_input, da9052_tsi, DA9052_ADC_TSI_XP); |
| 340 | static SENSOR_DEVICE_ATTR_RO(in70_label, label, DA9052_ADC_TSI_XP); |
| 341 | static SENSOR_DEVICE_ATTR_RO(in71_input, da9052_tsi, DA9052_ADC_TSI_XN); |
| 342 | static SENSOR_DEVICE_ATTR_RO(in71_label, label, DA9052_ADC_TSI_XN); |
| 343 | static SENSOR_DEVICE_ATTR_RO(in72_input, da9052_tsi, DA9052_ADC_TSI_YP); |
| 344 | static SENSOR_DEVICE_ATTR_RO(in72_label, label, DA9052_ADC_TSI_YP); |
| 345 | static SENSOR_DEVICE_ATTR_RO(in73_input, da9052_tsi, DA9052_ADC_TSI_YN); |
| 346 | static SENSOR_DEVICE_ATTR_RO(in73_label, label, DA9052_ADC_TSI_YN); |
Sebastian Reichel | 4f16cab | 2017-08-21 16:54:04 +0200 | [diff] [blame] | 347 | |
Guenter Roeck | 6db587b | 2018-12-10 14:02:04 -0800 | [diff] [blame] | 348 | static SENSOR_DEVICE_ATTR_RO(curr1_input, da9052_ich, DA9052_ADC_ICH); |
| 349 | static SENSOR_DEVICE_ATTR_RO(curr1_label, label, DA9052_ADC_ICH); |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 350 | |
Guenter Roeck | 6db587b | 2018-12-10 14:02:04 -0800 | [diff] [blame] | 351 | static SENSOR_DEVICE_ATTR_RO(temp2_input, da9052_tbat, DA9052_ADC_TBAT); |
| 352 | static SENSOR_DEVICE_ATTR_RO(temp2_label, label, DA9052_ADC_TBAT); |
| 353 | static SENSOR_DEVICE_ATTR_RO(temp8_input, da9052_tjunc, DA9052_ADC_TJUNC); |
| 354 | static SENSOR_DEVICE_ATTR_RO(temp8_label, label, DA9052_ADC_TJUNC); |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 355 | |
Axel Lin | 4222eb5 | 2014-07-09 09:20:41 +0800 | [diff] [blame] | 356 | static struct attribute *da9052_attrs[] = { |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 357 | &sensor_dev_attr_in0_input.dev_attr.attr, |
| 358 | &sensor_dev_attr_in0_label.dev_attr.attr, |
| 359 | &sensor_dev_attr_in3_input.dev_attr.attr, |
| 360 | &sensor_dev_attr_in3_label.dev_attr.attr, |
| 361 | &sensor_dev_attr_in4_input.dev_attr.attr, |
| 362 | &sensor_dev_attr_in4_label.dev_attr.attr, |
| 363 | &sensor_dev_attr_in5_input.dev_attr.attr, |
| 364 | &sensor_dev_attr_in5_label.dev_attr.attr, |
| 365 | &sensor_dev_attr_in6_input.dev_attr.attr, |
| 366 | &sensor_dev_attr_in6_label.dev_attr.attr, |
Sebastian Reichel | 4f16cab | 2017-08-21 16:54:04 +0200 | [diff] [blame] | 367 | &sensor_dev_attr_in70_input.dev_attr.attr, |
| 368 | &sensor_dev_attr_in70_label.dev_attr.attr, |
| 369 | &sensor_dev_attr_in71_input.dev_attr.attr, |
| 370 | &sensor_dev_attr_in71_label.dev_attr.attr, |
| 371 | &sensor_dev_attr_in72_input.dev_attr.attr, |
| 372 | &sensor_dev_attr_in72_label.dev_attr.attr, |
| 373 | &sensor_dev_attr_in73_input.dev_attr.attr, |
| 374 | &sensor_dev_attr_in73_label.dev_attr.attr, |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 375 | &sensor_dev_attr_in9_input.dev_attr.attr, |
| 376 | &sensor_dev_attr_in9_label.dev_attr.attr, |
| 377 | &sensor_dev_attr_curr1_input.dev_attr.attr, |
| 378 | &sensor_dev_attr_curr1_label.dev_attr.attr, |
| 379 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
| 380 | &sensor_dev_attr_temp2_label.dev_attr.attr, |
| 381 | &sensor_dev_attr_temp8_input.dev_attr.attr, |
| 382 | &sensor_dev_attr_temp8_label.dev_attr.attr, |
| 383 | NULL |
| 384 | }; |
| 385 | |
Sebastian Reichel | 4f16cab | 2017-08-21 16:54:04 +0200 | [diff] [blame] | 386 | static const struct attribute_group da9052_group = { |
| 387 | .attrs = da9052_attrs, |
| 388 | .is_visible = da9052_channel_is_visible, |
| 389 | }; |
| 390 | __ATTRIBUTE_GROUPS(da9052); |
| 391 | |
| 392 | static irqreturn_t da9052_tsi_datardy_irq(int irq, void *data) |
| 393 | { |
| 394 | struct da9052_hwmon *hwmon = data; |
| 395 | |
| 396 | complete(&hwmon->tsidone); |
| 397 | return IRQ_HANDLED; |
| 398 | } |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 399 | |
Bill Pemberton | 6c931ae | 2012-11-19 13:22:35 -0500 | [diff] [blame] | 400 | static int da9052_hwmon_probe(struct platform_device *pdev) |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 401 | { |
Axel Lin | 4222eb5 | 2014-07-09 09:20:41 +0800 | [diff] [blame] | 402 | struct device *dev = &pdev->dev; |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 403 | struct da9052_hwmon *hwmon; |
Axel Lin | 4222eb5 | 2014-07-09 09:20:41 +0800 | [diff] [blame] | 404 | struct device *hwmon_dev; |
Sebastian Reichel | 4f16cab | 2017-08-21 16:54:04 +0200 | [diff] [blame] | 405 | int err; |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 406 | |
Axel Lin | 4222eb5 | 2014-07-09 09:20:41 +0800 | [diff] [blame] | 407 | hwmon = devm_kzalloc(dev, sizeof(struct da9052_hwmon), GFP_KERNEL); |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 408 | if (!hwmon) |
| 409 | return -ENOMEM; |
| 410 | |
Sebastian Reichel | 4f16cab | 2017-08-21 16:54:04 +0200 | [diff] [blame] | 411 | platform_set_drvdata(pdev, hwmon); |
| 412 | |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 413 | mutex_init(&hwmon->hwmon_lock); |
| 414 | hwmon->da9052 = dev_get_drvdata(pdev->dev.parent); |
| 415 | |
Sebastian Reichel | 4f16cab | 2017-08-21 16:54:04 +0200 | [diff] [blame] | 416 | init_completion(&hwmon->tsidone); |
| 417 | |
| 418 | hwmon->tsi_as_adc = |
| 419 | device_property_read_bool(pdev->dev.parent, "dlg,tsi-as-adc"); |
| 420 | |
| 421 | if (hwmon->tsi_as_adc) { |
| 422 | hwmon->tsiref = devm_regulator_get(pdev->dev.parent, "tsiref"); |
| 423 | if (IS_ERR(hwmon->tsiref)) { |
| 424 | err = PTR_ERR(hwmon->tsiref); |
| 425 | dev_err(&pdev->dev, "failed to get tsiref: %d", err); |
| 426 | return err; |
| 427 | } |
| 428 | |
| 429 | err = regulator_enable(hwmon->tsiref); |
| 430 | if (err) |
| 431 | return err; |
| 432 | |
| 433 | hwmon->tsiref_mv = regulator_get_voltage(hwmon->tsiref); |
| 434 | if (hwmon->tsiref_mv < 0) { |
| 435 | err = hwmon->tsiref_mv; |
| 436 | goto exit_regulator; |
| 437 | } |
| 438 | |
| 439 | /* convert from microvolt (DT) to millivolt (hwmon) */ |
| 440 | hwmon->tsiref_mv /= 1000; |
| 441 | |
| 442 | /* TSIREF limits from datasheet */ |
| 443 | if (hwmon->tsiref_mv < 1800 || hwmon->tsiref_mv > 2600) { |
| 444 | dev_err(hwmon->da9052->dev, "invalid TSIREF voltage: %d", |
| 445 | hwmon->tsiref_mv); |
| 446 | err = -ENXIO; |
| 447 | goto exit_regulator; |
| 448 | } |
| 449 | |
| 450 | /* disable touchscreen features */ |
| 451 | da9052_reg_write(hwmon->da9052, DA9052_TSI_CONT_A_REG, 0x00); |
| 452 | |
Martyn Welch | b16918a | 2017-10-19 16:51:44 +0100 | [diff] [blame] | 453 | /* Sample every 1ms */ |
| 454 | da9052_reg_update(hwmon->da9052, DA9052_ADC_CONT_REG, |
| 455 | DA9052_ADCCONT_ADCMODE, |
| 456 | DA9052_ADCCONT_ADCMODE); |
| 457 | |
Sebastian Reichel | 4f16cab | 2017-08-21 16:54:04 +0200 | [diff] [blame] | 458 | err = da9052_request_irq(hwmon->da9052, DA9052_IRQ_TSIREADY, |
| 459 | "tsiready-irq", da9052_tsi_datardy_irq, |
| 460 | hwmon); |
| 461 | if (err) { |
| 462 | dev_err(&pdev->dev, "Failed to register TSIRDY IRQ: %d", |
| 463 | err); |
| 464 | goto exit_regulator; |
| 465 | } |
| 466 | } |
| 467 | |
Axel Lin | 4222eb5 | 2014-07-09 09:20:41 +0800 | [diff] [blame] | 468 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, "da9052", |
| 469 | hwmon, |
| 470 | da9052_groups); |
Sebastian Reichel | 4f16cab | 2017-08-21 16:54:04 +0200 | [diff] [blame] | 471 | err = PTR_ERR_OR_ZERO(hwmon_dev); |
| 472 | if (err) |
| 473 | goto exit_irq; |
| 474 | |
| 475 | return 0; |
| 476 | |
| 477 | exit_irq: |
| 478 | if (hwmon->tsi_as_adc) |
| 479 | da9052_free_irq(hwmon->da9052, DA9052_IRQ_TSIREADY, hwmon); |
| 480 | exit_regulator: |
| 481 | if (hwmon->tsiref) |
| 482 | regulator_disable(hwmon->tsiref); |
| 483 | |
| 484 | return err; |
| 485 | } |
| 486 | |
| 487 | static int da9052_hwmon_remove(struct platform_device *pdev) |
| 488 | { |
| 489 | struct da9052_hwmon *hwmon = platform_get_drvdata(pdev); |
| 490 | |
| 491 | if (hwmon->tsi_as_adc) { |
| 492 | da9052_free_irq(hwmon->da9052, DA9052_IRQ_TSIREADY, hwmon); |
| 493 | regulator_disable(hwmon->tsiref); |
| 494 | } |
| 495 | |
| 496 | return 0; |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | static struct platform_driver da9052_hwmon_driver = { |
| 500 | .probe = da9052_hwmon_probe, |
Sebastian Reichel | 4f16cab | 2017-08-21 16:54:04 +0200 | [diff] [blame] | 501 | .remove = da9052_hwmon_remove, |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 502 | .driver = { |
| 503 | .name = "da9052-hwmon", |
Ashish Jangam | e41f643 | 2012-03-17 15:34:41 +0530 | [diff] [blame] | 504 | }, |
| 505 | }; |
| 506 | |
| 507 | module_platform_driver(da9052_hwmon_driver); |
| 508 | |
| 509 | MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>"); |
| 510 | MODULE_DESCRIPTION("DA9052 HWMON driver"); |
| 511 | MODULE_LICENSE("GPL"); |
| 512 | MODULE_ALIAS("platform:da9052-hwmon"); |