2 * HWMON Driver for Dialog DA9052
4 * Copyright(c) 2012 Dialog Semiconductor Ltd.
6 * Author: David Dajun Chen <dchen@diasemi.com>
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.
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>
23 #include <linux/property.h>
25 #include <linux/mfd/da9052/da9052.h>
26 #include <linux/mfd/da9052/reg.h>
27 #include <linux/regulator/consumer.h>
30 struct da9052 *da9052;
31 struct mutex hwmon_lock;
34 struct regulator *tsiref;
35 struct completion tsidone;
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",
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-",
50 [DA9052_ADC_TJUNC] = "BATTERY JUNCTION TEMP",
51 [DA9052_ADC_VBBAT] = "BACK-UP BATTERY VOLTAGE",
54 /* Conversion function for VDDOUT and VBAT */
55 static inline int volt_reg_to_mv(int value)
57 return DIV_ROUND_CLOSEST(value * 2000, 1023) + 2500;
60 /* Conversion function for ADC channels 4, 5 and 6 */
61 static inline int input_reg_to_mv(int value)
63 return DIV_ROUND_CLOSEST(value * 2500, 1023);
66 /* Conversion function for VBBAT */
67 static inline int vbbat_reg_to_mv(int value)
69 return DIV_ROUND_CLOSEST(value * 5000, 1023);
72 static inline int input_tsireg_to_mv(struct da9052_hwmon *hwmon, int value)
74 return DIV_ROUND_CLOSEST(value * hwmon->tsiref_mv, 1023);
77 static inline int da9052_enable_vddout_channel(struct da9052 *da9052)
79 return da9052_reg_update(da9052, DA9052_ADC_CONT_REG,
80 DA9052_ADCCONT_AUTOVDDEN,
81 DA9052_ADCCONT_AUTOVDDEN);
84 static inline int da9052_disable_vddout_channel(struct da9052 *da9052)
86 return da9052_reg_update(da9052, DA9052_ADC_CONT_REG,
87 DA9052_ADCCONT_AUTOVDDEN, 0);
90 static ssize_t da9052_read_vddout(struct device *dev,
91 struct device_attribute *devattr, char *buf)
93 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
96 mutex_lock(&hwmon->hwmon_lock);
98 ret = da9052_enable_vddout_channel(hwmon->da9052);
102 vdd = da9052_reg_read(hwmon->da9052, DA9052_VDD_RES_REG);
105 goto hwmon_err_release;
108 ret = da9052_disable_vddout_channel(hwmon->da9052);
112 mutex_unlock(&hwmon->hwmon_lock);
113 return sprintf(buf, "%d\n", volt_reg_to_mv(vdd));
116 da9052_disable_vddout_channel(hwmon->da9052);
118 mutex_unlock(&hwmon->hwmon_lock);
122 static ssize_t da9052_read_ich(struct device *dev,
123 struct device_attribute *devattr, char *buf)
125 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
128 ret = da9052_reg_read(hwmon->da9052, DA9052_ICHG_AV_REG);
132 /* Equivalent to 3.9mA/bit in register ICHG_AV */
133 return sprintf(buf, "%d\n", DIV_ROUND_CLOSEST(ret * 39, 10));
136 static ssize_t da9052_read_tbat(struct device *dev,
137 struct device_attribute *devattr, char *buf)
139 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
141 return sprintf(buf, "%d\n", da9052_adc_read_temp(hwmon->da9052));
144 static ssize_t da9052_read_vbat(struct device *dev,
145 struct device_attribute *devattr, char *buf)
147 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
150 ret = da9052_adc_manual_read(hwmon->da9052, DA9052_ADC_VBAT);
154 return sprintf(buf, "%d\n", volt_reg_to_mv(ret));
157 static ssize_t da9052_read_misc_channel(struct device *dev,
158 struct device_attribute *devattr,
161 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
162 int channel = to_sensor_dev_attr(devattr)->index;
165 ret = da9052_adc_manual_read(hwmon->da9052, channel);
169 return sprintf(buf, "%d\n", input_reg_to_mv(ret));
172 static int da9052_request_tsi_read(struct da9052_hwmon *hwmon, int channel)
174 u8 val = DA9052_TSICONTB_TSIMAN;
177 case DA9052_ADC_TSI_XP:
178 val |= DA9052_TSICONTB_TSIMUX_XP;
180 case DA9052_ADC_TSI_YP:
181 val |= DA9052_TSICONTB_TSIMUX_YP;
183 case DA9052_ADC_TSI_XN:
184 val |= DA9052_TSICONTB_TSIMUX_XN;
186 case DA9052_ADC_TSI_YN:
187 val |= DA9052_TSICONTB_TSIMUX_YN;
191 return da9052_reg_write(hwmon->da9052, DA9052_TSI_CONT_B_REG, val);
194 static int da9052_get_tsi_result(struct da9052_hwmon *hwmon, int channel)
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);
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;
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;
226 static ssize_t __da9052_read_tsi(struct device *dev, int channel)
228 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
231 reinit_completion(&hwmon->tsidone);
233 ret = da9052_request_tsi_read(hwmon, channel);
237 /* Wait for an conversion done interrupt */
238 if (!wait_for_completion_timeout(&hwmon->tsidone,
239 msecs_to_jiffies(500)))
242 return da9052_get_tsi_result(hwmon, channel);
245 static ssize_t da9052_read_tsi(struct device *dev,
246 struct device_attribute *devattr,
249 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
250 int channel = to_sensor_dev_attr(devattr)->index;
253 mutex_lock(&hwmon->hwmon_lock);
254 ret = __da9052_read_tsi(dev, channel);
255 mutex_unlock(&hwmon->hwmon_lock);
260 return sprintf(buf, "%d\n", input_tsireg_to_mv(hwmon, ret));
263 static ssize_t da9052_read_tjunc(struct device *dev,
264 struct device_attribute *devattr, char *buf)
266 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
270 tjunc = da9052_reg_read(hwmon->da9052, DA9052_TJUNC_RES_REG);
274 toffset = da9052_reg_read(hwmon->da9052, DA9052_T_OFFSET_REG);
279 * Degrees celsius = 1.708 * (TJUNC_RES - T_OFFSET) - 108.8
280 * T_OFFSET is a trim value used to improve accuracy of the result
282 return sprintf(buf, "%d\n", 1708 * (tjunc - toffset) - 108800);
285 static ssize_t da9052_read_vbbat(struct device *dev,
286 struct device_attribute *devattr, char *buf)
288 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
291 ret = da9052_adc_manual_read(hwmon->da9052, DA9052_ADC_VBBAT);
295 return sprintf(buf, "%d\n", vbbat_reg_to_mv(ret));
298 static ssize_t show_label(struct device *dev,
299 struct device_attribute *devattr, char *buf)
301 return sprintf(buf, "%s\n",
302 input_names[to_sensor_dev_attr(devattr)->index]);
305 static umode_t da9052_channel_is_visible(struct kobject *kobj,
306 struct attribute *attr, int index)
308 struct device *dev = container_of(kobj, struct device, kobj);
309 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
310 struct device_attribute *dattr = container_of(attr,
311 struct device_attribute, attr);
312 struct sensor_device_attribute *sattr = to_sensor_dev_attr(dattr);
314 if (!hwmon->tsi_as_adc) {
315 switch (sattr->index) {
316 case DA9052_ADC_TSI_XP:
317 case DA9052_ADC_TSI_YP:
318 case DA9052_ADC_TSI_XN:
319 case DA9052_ADC_TSI_YN:
327 static SENSOR_DEVICE_ATTR(in0_input, 0444, da9052_read_vddout, NULL,
329 static SENSOR_DEVICE_ATTR(in0_label, 0444, show_label, NULL,
331 static SENSOR_DEVICE_ATTR(in3_input, 0444, da9052_read_vbat, NULL,
333 static SENSOR_DEVICE_ATTR(in3_label, 0444, show_label, NULL,
335 static SENSOR_DEVICE_ATTR(in4_input, 0444, da9052_read_misc_channel, NULL,
337 static SENSOR_DEVICE_ATTR(in4_label, 0444, show_label, NULL,
339 static SENSOR_DEVICE_ATTR(in5_input, 0444, da9052_read_misc_channel, NULL,
341 static SENSOR_DEVICE_ATTR(in5_label, 0444, show_label, NULL,
343 static SENSOR_DEVICE_ATTR(in6_input, 0444, da9052_read_misc_channel, NULL,
345 static SENSOR_DEVICE_ATTR(in6_label, 0444, show_label, NULL,
347 static SENSOR_DEVICE_ATTR(in9_input, 0444, da9052_read_vbbat, NULL,
349 static SENSOR_DEVICE_ATTR(in9_label, 0444, show_label, NULL,
352 static SENSOR_DEVICE_ATTR(in70_input, 0444, da9052_read_tsi, NULL,
354 static SENSOR_DEVICE_ATTR(in70_label, 0444, show_label, NULL,
356 static SENSOR_DEVICE_ATTR(in71_input, 0444, da9052_read_tsi, NULL,
358 static SENSOR_DEVICE_ATTR(in71_label, 0444, show_label, NULL,
360 static SENSOR_DEVICE_ATTR(in72_input, 0444, da9052_read_tsi, NULL,
362 static SENSOR_DEVICE_ATTR(in72_label, 0444, show_label, NULL,
364 static SENSOR_DEVICE_ATTR(in73_input, 0444, da9052_read_tsi, NULL,
366 static SENSOR_DEVICE_ATTR(in73_label, 0444, show_label, NULL,
369 static SENSOR_DEVICE_ATTR(curr1_input, 0444, da9052_read_ich, NULL,
371 static SENSOR_DEVICE_ATTR(curr1_label, 0444, show_label, NULL,
374 static SENSOR_DEVICE_ATTR(temp2_input, 0444, da9052_read_tbat, NULL,
376 static SENSOR_DEVICE_ATTR(temp2_label, 0444, show_label, NULL,
378 static SENSOR_DEVICE_ATTR(temp8_input, 0444, da9052_read_tjunc, NULL,
380 static SENSOR_DEVICE_ATTR(temp8_label, 0444, show_label, NULL,
383 static struct attribute *da9052_attrs[] = {
384 &sensor_dev_attr_in0_input.dev_attr.attr,
385 &sensor_dev_attr_in0_label.dev_attr.attr,
386 &sensor_dev_attr_in3_input.dev_attr.attr,
387 &sensor_dev_attr_in3_label.dev_attr.attr,
388 &sensor_dev_attr_in4_input.dev_attr.attr,
389 &sensor_dev_attr_in4_label.dev_attr.attr,
390 &sensor_dev_attr_in5_input.dev_attr.attr,
391 &sensor_dev_attr_in5_label.dev_attr.attr,
392 &sensor_dev_attr_in6_input.dev_attr.attr,
393 &sensor_dev_attr_in6_label.dev_attr.attr,
394 &sensor_dev_attr_in70_input.dev_attr.attr,
395 &sensor_dev_attr_in70_label.dev_attr.attr,
396 &sensor_dev_attr_in71_input.dev_attr.attr,
397 &sensor_dev_attr_in71_label.dev_attr.attr,
398 &sensor_dev_attr_in72_input.dev_attr.attr,
399 &sensor_dev_attr_in72_label.dev_attr.attr,
400 &sensor_dev_attr_in73_input.dev_attr.attr,
401 &sensor_dev_attr_in73_label.dev_attr.attr,
402 &sensor_dev_attr_in9_input.dev_attr.attr,
403 &sensor_dev_attr_in9_label.dev_attr.attr,
404 &sensor_dev_attr_curr1_input.dev_attr.attr,
405 &sensor_dev_attr_curr1_label.dev_attr.attr,
406 &sensor_dev_attr_temp2_input.dev_attr.attr,
407 &sensor_dev_attr_temp2_label.dev_attr.attr,
408 &sensor_dev_attr_temp8_input.dev_attr.attr,
409 &sensor_dev_attr_temp8_label.dev_attr.attr,
413 static const struct attribute_group da9052_group = {
414 .attrs = da9052_attrs,
415 .is_visible = da9052_channel_is_visible,
417 __ATTRIBUTE_GROUPS(da9052);
419 static irqreturn_t da9052_tsi_datardy_irq(int irq, void *data)
421 struct da9052_hwmon *hwmon = data;
423 complete(&hwmon->tsidone);
427 static int da9052_hwmon_probe(struct platform_device *pdev)
429 struct device *dev = &pdev->dev;
430 struct da9052_hwmon *hwmon;
431 struct device *hwmon_dev;
434 hwmon = devm_kzalloc(dev, sizeof(struct da9052_hwmon), GFP_KERNEL);
438 platform_set_drvdata(pdev, hwmon);
440 mutex_init(&hwmon->hwmon_lock);
441 hwmon->da9052 = dev_get_drvdata(pdev->dev.parent);
443 init_completion(&hwmon->tsidone);
446 device_property_read_bool(pdev->dev.parent, "dlg,tsi-as-adc");
448 if (hwmon->tsi_as_adc) {
449 hwmon->tsiref = devm_regulator_get(pdev->dev.parent, "tsiref");
450 if (IS_ERR(hwmon->tsiref)) {
451 err = PTR_ERR(hwmon->tsiref);
452 dev_err(&pdev->dev, "failed to get tsiref: %d", err);
456 err = regulator_enable(hwmon->tsiref);
460 hwmon->tsiref_mv = regulator_get_voltage(hwmon->tsiref);
461 if (hwmon->tsiref_mv < 0) {
462 err = hwmon->tsiref_mv;
466 /* convert from microvolt (DT) to millivolt (hwmon) */
467 hwmon->tsiref_mv /= 1000;
469 /* TSIREF limits from datasheet */
470 if (hwmon->tsiref_mv < 1800 || hwmon->tsiref_mv > 2600) {
471 dev_err(hwmon->da9052->dev, "invalid TSIREF voltage: %d",
477 /* disable touchscreen features */
478 da9052_reg_write(hwmon->da9052, DA9052_TSI_CONT_A_REG, 0x00);
480 /* Sample every 1ms */
481 da9052_reg_update(hwmon->da9052, DA9052_ADC_CONT_REG,
482 DA9052_ADCCONT_ADCMODE,
483 DA9052_ADCCONT_ADCMODE);
485 err = da9052_request_irq(hwmon->da9052, DA9052_IRQ_TSIREADY,
486 "tsiready-irq", da9052_tsi_datardy_irq,
489 dev_err(&pdev->dev, "Failed to register TSIRDY IRQ: %d",
495 hwmon_dev = devm_hwmon_device_register_with_groups(dev, "da9052",
498 err = PTR_ERR_OR_ZERO(hwmon_dev);
505 if (hwmon->tsi_as_adc)
506 da9052_free_irq(hwmon->da9052, DA9052_IRQ_TSIREADY, hwmon);
509 regulator_disable(hwmon->tsiref);
514 static int da9052_hwmon_remove(struct platform_device *pdev)
516 struct da9052_hwmon *hwmon = platform_get_drvdata(pdev);
518 if (hwmon->tsi_as_adc) {
519 da9052_free_irq(hwmon->da9052, DA9052_IRQ_TSIREADY, hwmon);
520 regulator_disable(hwmon->tsiref);
526 static struct platform_driver da9052_hwmon_driver = {
527 .probe = da9052_hwmon_probe,
528 .remove = da9052_hwmon_remove,
530 .name = "da9052-hwmon",
534 module_platform_driver(da9052_hwmon_driver);
536 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
537 MODULE_DESCRIPTION("DA9052 HWMON driver");
538 MODULE_LICENSE("GPL");
539 MODULE_ALIAS("platform:da9052-hwmon");