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>
24 #include <linux/mfd/da9052/da9052.h>
25 #include <linux/mfd/da9052/reg.h>
28 struct da9052 *da9052;
29 struct mutex hwmon_lock;
32 static const char * const input_names[] = {
33 [DA9052_ADC_VDDOUT] = "VDDOUT",
34 [DA9052_ADC_ICH] = "CHARGING CURRENT",
35 [DA9052_ADC_TBAT] = "BATTERY TEMP",
36 [DA9052_ADC_VBAT] = "BATTERY VOLTAGE",
37 [DA9052_ADC_IN4] = "ADC IN4",
38 [DA9052_ADC_IN5] = "ADC IN5",
39 [DA9052_ADC_IN6] = "ADC IN6",
40 [DA9052_ADC_TJUNC] = "BATTERY JUNCTION TEMP",
41 [DA9052_ADC_VBBAT] = "BACK-UP BATTERY VOLTAGE",
44 /* Conversion function for VDDOUT and VBAT */
45 static inline int volt_reg_to_mv(int value)
47 return DIV_ROUND_CLOSEST(value * 2000, 1023) + 2500;
50 /* Conversion function for ADC channels 4, 5 and 6 */
51 static inline int input_reg_to_mv(int value)
53 return DIV_ROUND_CLOSEST(value * 2500, 1023);
56 /* Conversion function for VBBAT */
57 static inline int vbbat_reg_to_mv(int value)
59 return DIV_ROUND_CLOSEST(value * 5000, 1023);
62 static inline int da9052_enable_vddout_channel(struct da9052 *da9052)
64 return da9052_reg_update(da9052, DA9052_ADC_CONT_REG,
65 DA9052_ADCCONT_AUTOVDDEN,
66 DA9052_ADCCONT_AUTOVDDEN);
69 static inline int da9052_disable_vddout_channel(struct da9052 *da9052)
71 return da9052_reg_update(da9052, DA9052_ADC_CONT_REG,
72 DA9052_ADCCONT_AUTOVDDEN, 0);
75 static ssize_t da9052_read_vddout(struct device *dev,
76 struct device_attribute *devattr, char *buf)
78 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
81 mutex_lock(&hwmon->hwmon_lock);
83 ret = da9052_enable_vddout_channel(hwmon->da9052);
87 vdd = da9052_reg_read(hwmon->da9052, DA9052_VDD_RES_REG);
90 goto hwmon_err_release;
93 ret = da9052_disable_vddout_channel(hwmon->da9052);
97 mutex_unlock(&hwmon->hwmon_lock);
98 return sprintf(buf, "%d\n", volt_reg_to_mv(vdd));
101 da9052_disable_vddout_channel(hwmon->da9052);
103 mutex_unlock(&hwmon->hwmon_lock);
107 static ssize_t da9052_read_ich(struct device *dev,
108 struct device_attribute *devattr, char *buf)
110 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
113 ret = da9052_reg_read(hwmon->da9052, DA9052_ICHG_AV_REG);
117 /* Equivalent to 3.9mA/bit in register ICHG_AV */
118 return sprintf(buf, "%d\n", DIV_ROUND_CLOSEST(ret * 39, 10));
121 static ssize_t da9052_read_tbat(struct device *dev,
122 struct device_attribute *devattr, char *buf)
124 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
126 return sprintf(buf, "%d\n", da9052_adc_read_temp(hwmon->da9052));
129 static ssize_t da9052_read_vbat(struct device *dev,
130 struct device_attribute *devattr, char *buf)
132 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
135 ret = da9052_adc_manual_read(hwmon->da9052, DA9052_ADC_VBAT);
139 return sprintf(buf, "%d\n", volt_reg_to_mv(ret));
142 static ssize_t da9052_read_misc_channel(struct device *dev,
143 struct device_attribute *devattr,
146 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
147 int channel = to_sensor_dev_attr(devattr)->index;
150 ret = da9052_adc_manual_read(hwmon->da9052, channel);
154 return sprintf(buf, "%d\n", input_reg_to_mv(ret));
157 static ssize_t da9052_read_tjunc(struct device *dev,
158 struct device_attribute *devattr, char *buf)
160 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
164 tjunc = da9052_reg_read(hwmon->da9052, DA9052_TJUNC_RES_REG);
168 toffset = da9052_reg_read(hwmon->da9052, DA9052_T_OFFSET_REG);
173 * Degrees celsius = 1.708 * (TJUNC_RES - T_OFFSET) - 108.8
174 * T_OFFSET is a trim value used to improve accuracy of the result
176 return sprintf(buf, "%d\n", 1708 * (tjunc - toffset) - 108800);
179 static ssize_t da9052_read_vbbat(struct device *dev,
180 struct device_attribute *devattr, char *buf)
182 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
185 ret = da9052_adc_manual_read(hwmon->da9052, DA9052_ADC_VBBAT);
189 return sprintf(buf, "%d\n", vbbat_reg_to_mv(ret));
192 static ssize_t show_label(struct device *dev,
193 struct device_attribute *devattr, char *buf)
195 return sprintf(buf, "%s\n",
196 input_names[to_sensor_dev_attr(devattr)->index]);
199 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, da9052_read_vddout, NULL,
201 static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, show_label, NULL,
203 static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, da9052_read_vbat, NULL,
205 static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL,
207 static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, da9052_read_misc_channel, NULL,
209 static SENSOR_DEVICE_ATTR(in4_label, S_IRUGO, show_label, NULL,
211 static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, da9052_read_misc_channel, NULL,
213 static SENSOR_DEVICE_ATTR(in5_label, S_IRUGO, show_label, NULL,
215 static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, da9052_read_misc_channel, NULL,
217 static SENSOR_DEVICE_ATTR(in6_label, S_IRUGO, show_label, NULL,
219 static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, da9052_read_vbbat, NULL,
221 static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL,
224 static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, da9052_read_ich, NULL,
226 static SENSOR_DEVICE_ATTR(curr1_label, S_IRUGO, show_label, NULL,
229 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, da9052_read_tbat, NULL,
231 static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, show_label, NULL,
233 static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO, da9052_read_tjunc, NULL,
235 static SENSOR_DEVICE_ATTR(temp8_label, S_IRUGO, show_label, NULL,
238 static struct attribute *da9052_attrs[] = {
239 &sensor_dev_attr_in0_input.dev_attr.attr,
240 &sensor_dev_attr_in0_label.dev_attr.attr,
241 &sensor_dev_attr_in3_input.dev_attr.attr,
242 &sensor_dev_attr_in3_label.dev_attr.attr,
243 &sensor_dev_attr_in4_input.dev_attr.attr,
244 &sensor_dev_attr_in4_label.dev_attr.attr,
245 &sensor_dev_attr_in5_input.dev_attr.attr,
246 &sensor_dev_attr_in5_label.dev_attr.attr,
247 &sensor_dev_attr_in6_input.dev_attr.attr,
248 &sensor_dev_attr_in6_label.dev_attr.attr,
249 &sensor_dev_attr_in9_input.dev_attr.attr,
250 &sensor_dev_attr_in9_label.dev_attr.attr,
251 &sensor_dev_attr_curr1_input.dev_attr.attr,
252 &sensor_dev_attr_curr1_label.dev_attr.attr,
253 &sensor_dev_attr_temp2_input.dev_attr.attr,
254 &sensor_dev_attr_temp2_label.dev_attr.attr,
255 &sensor_dev_attr_temp8_input.dev_attr.attr,
256 &sensor_dev_attr_temp8_label.dev_attr.attr,
260 ATTRIBUTE_GROUPS(da9052);
262 static int da9052_hwmon_probe(struct platform_device *pdev)
264 struct device *dev = &pdev->dev;
265 struct da9052_hwmon *hwmon;
266 struct device *hwmon_dev;
268 hwmon = devm_kzalloc(dev, sizeof(struct da9052_hwmon), GFP_KERNEL);
272 mutex_init(&hwmon->hwmon_lock);
273 hwmon->da9052 = dev_get_drvdata(pdev->dev.parent);
275 hwmon_dev = devm_hwmon_device_register_with_groups(dev, "da9052",
278 return PTR_ERR_OR_ZERO(hwmon_dev);
281 static struct platform_driver da9052_hwmon_driver = {
282 .probe = da9052_hwmon_probe,
284 .name = "da9052-hwmon",
285 .owner = THIS_MODULE,
289 module_platform_driver(da9052_hwmon_driver);
291 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
292 MODULE_DESCRIPTION("DA9052 HWMON driver");
293 MODULE_LICENSE("GPL");
294 MODULE_ALIAS("platform:da9052-hwmon");