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/delay.h>
16 #include <linux/err.h>
17 #include <linux/hwmon.h>
18 #include <linux/hwmon-sysfs.h>
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/platform_device.h>
25 #include <linux/mfd/da9052/da9052.h>
26 #include <linux/mfd/da9052/reg.h>
29 struct da9052 *da9052;
30 struct device *class_device;
31 struct mutex hwmon_lock;
34 static const char * const input_names[] = {
35 [DA9052_ADC_VDDOUT] = "VDDOUT",
36 [DA9052_ADC_ICH] = "CHARGING CURRENT",
37 [DA9052_ADC_TBAT] = "BATTERY TEMP",
38 [DA9052_ADC_VBAT] = "BATTERY VOLTAGE",
39 [DA9052_ADC_IN4] = "ADC IN4",
40 [DA9052_ADC_IN5] = "ADC IN5",
41 [DA9052_ADC_IN6] = "ADC IN6",
42 [DA9052_ADC_TJUNC] = "BATTERY JUNCTION TEMP",
43 [DA9052_ADC_VBBAT] = "BACK-UP BATTERY VOLTAGE",
46 /* Conversion function for VDDOUT and VBAT */
47 static inline int volt_reg_to_mV(int value)
49 return DIV_ROUND_CLOSEST(value * 1000, 512) + 2500;
52 /* Conversion function for ADC channels 4, 5 and 6 */
53 static inline int input_reg_to_mV(int value)
55 return DIV_ROUND_CLOSEST(value * 2500, 1023);
58 /* Conversion function for VBBAT */
59 static inline int vbbat_reg_to_mV(int value)
61 return DIV_ROUND_CLOSEST(value * 2500, 512);
64 static int da9052_enable_vddout_channel(struct da9052 *da9052)
68 ret = da9052_reg_read(da9052, DA9052_ADC_CONT_REG);
72 ret |= DA9052_ADCCONT_AUTOVDDEN;
74 return da9052_reg_write(da9052, DA9052_ADC_CONT_REG, ret);
77 static int da9052_disable_vddout_channel(struct da9052 *da9052)
81 ret = da9052_reg_read(da9052, DA9052_ADC_CONT_REG);
85 ret &= ~DA9052_ADCCONT_AUTOVDDEN;
87 return da9052_reg_write(da9052, DA9052_ADC_CONT_REG, ret);
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 ssize_t da9052_read_tjunc(struct device *dev,
173 struct device_attribute *devattr, char *buf)
175 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
179 tjunc = da9052_reg_read(hwmon->da9052, DA9052_TJUNC_RES_REG);
183 toffset = da9052_reg_read(hwmon->da9052, DA9052_T_OFFSET_REG);
188 * Degrees celsius = 1.708 * (TJUNC_RES - T_OFFSET) - 108.8
189 * T_OFFSET is a trim value used to improve accuracy of the result
191 return sprintf(buf, "%d\n", 1708 * (tjunc - toffset) - 108800);
194 static ssize_t da9052_read_vbbat(struct device *dev,
195 struct device_attribute *devattr, char *buf)
197 struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
200 ret = da9052_adc_manual_read(hwmon->da9052, DA9052_ADC_VBBAT);
204 return sprintf(buf, "%d\n", vbbat_reg_to_mV(ret));
207 static ssize_t da9052_hwmon_show_name(struct device *dev,
208 struct device_attribute *devattr,
211 return sprintf(buf, "da9052-hwmon\n");
214 static ssize_t show_label(struct device *dev,
215 struct device_attribute *devattr, char *buf)
217 return sprintf(buf, "%s\n",
218 input_names[to_sensor_dev_attr(devattr)->index]);
221 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, da9052_read_vddout, NULL,
223 static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, show_label, NULL,
225 static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, da9052_read_vbat, NULL,
227 static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL,
229 static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, da9052_read_misc_channel, NULL,
231 static SENSOR_DEVICE_ATTR(in4_label, S_IRUGO, show_label, NULL,
233 static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, da9052_read_misc_channel, NULL,
235 static SENSOR_DEVICE_ATTR(in5_label, S_IRUGO, show_label, NULL,
237 static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, da9052_read_misc_channel, NULL,
239 static SENSOR_DEVICE_ATTR(in6_label, S_IRUGO, show_label, NULL,
241 static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, da9052_read_vbbat, NULL,
243 static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL,
246 static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, da9052_read_ich, NULL,
248 static SENSOR_DEVICE_ATTR(curr1_label, S_IRUGO, show_label, NULL,
251 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, da9052_read_tbat, NULL,
253 static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, show_label, NULL,
255 static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO, da9052_read_tjunc, NULL,
257 static SENSOR_DEVICE_ATTR(temp8_label, S_IRUGO, show_label, NULL,
260 static DEVICE_ATTR(name, S_IRUGO, da9052_hwmon_show_name, NULL);
262 static struct attribute *da9052_attr[] = {
264 &sensor_dev_attr_in0_input.dev_attr.attr,
265 &sensor_dev_attr_in0_label.dev_attr.attr,
266 &sensor_dev_attr_in3_input.dev_attr.attr,
267 &sensor_dev_attr_in3_label.dev_attr.attr,
268 &sensor_dev_attr_in4_input.dev_attr.attr,
269 &sensor_dev_attr_in4_label.dev_attr.attr,
270 &sensor_dev_attr_in5_input.dev_attr.attr,
271 &sensor_dev_attr_in5_label.dev_attr.attr,
272 &sensor_dev_attr_in6_input.dev_attr.attr,
273 &sensor_dev_attr_in6_label.dev_attr.attr,
274 &sensor_dev_attr_in9_input.dev_attr.attr,
275 &sensor_dev_attr_in9_label.dev_attr.attr,
276 &sensor_dev_attr_curr1_input.dev_attr.attr,
277 &sensor_dev_attr_curr1_label.dev_attr.attr,
278 &sensor_dev_attr_temp2_input.dev_attr.attr,
279 &sensor_dev_attr_temp2_label.dev_attr.attr,
280 &sensor_dev_attr_temp8_input.dev_attr.attr,
281 &sensor_dev_attr_temp8_label.dev_attr.attr,
285 static const struct attribute_group da9052_attr_group = {.attrs = da9052_attr};
287 static int __devinit da9052_hwmon_probe(struct platform_device *pdev)
289 struct da9052_hwmon *hwmon;
292 hwmon = devm_kzalloc(&pdev->dev, sizeof(struct da9052_hwmon),
297 mutex_init(&hwmon->hwmon_lock);
298 hwmon->da9052 = dev_get_drvdata(pdev->dev.parent);
300 platform_set_drvdata(pdev, hwmon);
302 ret = sysfs_create_group(&pdev->dev.kobj, &da9052_attr_group);
306 hwmon->class_device = hwmon_device_register(&pdev->dev);
307 if (IS_ERR(hwmon->class_device)) {
308 ret = PTR_ERR(hwmon->class_device);
315 sysfs_remove_group(&pdev->dev.kobj, &da9052_attr_group);
320 static int __devexit da9052_hwmon_remove(struct platform_device *pdev)
322 struct da9052_hwmon *hwmon = platform_get_drvdata(pdev);
324 hwmon_device_unregister(hwmon->class_device);
325 sysfs_remove_group(&pdev->dev.kobj, &da9052_attr_group);
330 static struct platform_driver da9052_hwmon_driver = {
331 .probe = da9052_hwmon_probe,
332 .remove = __devexit_p(da9052_hwmon_remove),
334 .name = "da9052-hwmon",
335 .owner = THIS_MODULE,
339 module_platform_driver(da9052_hwmon_driver);
341 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
342 MODULE_DESCRIPTION("DA9052 HWMON driver");
343 MODULE_LICENSE("GPL");
344 MODULE_ALIAS("platform:da9052-hwmon");