blob: 0018c7dd0097de5045f646d98e715713ea7edba4 [file] [log] [blame]
Keerthy0070bdd2011-03-01 19:12:36 +05301/*
2 *
3 * TWL4030 MADC Hwmon driver-This driver monitors the real time
4 * conversion of analog signals like battery temperature,
5 * battery type, battery level etc. User can ask for the conversion on a
6 * particular channel using the sysfs nodes.
7 *
8 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
9 * J Keerthy <j-keerthy@ti.com>
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * version 2 as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23 * 02110-1301 USA
24 *
25 */
26#include <linux/init.h>
27#include <linux/module.h>
28#include <linux/kernel.h>
29#include <linux/i2c/twl.h>
30#include <linux/device.h>
31#include <linux/platform_device.h>
32#include <linux/i2c/twl4030-madc.h>
33#include <linux/hwmon.h>
34#include <linux/hwmon-sysfs.h>
35#include <linux/stddef.h>
36#include <linux/sysfs.h>
37#include <linux/err.h>
38#include <linux/types.h>
39
40/*
41 * sysfs hook function
42 */
43static ssize_t madc_read(struct device *dev,
44 struct device_attribute *devattr, char *buf)
45{
46 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
47 struct twl4030_madc_request req;
48 long val;
49
50 req.channels = (1 << attr->index);
51 req.method = TWL4030_MADC_SW2;
52 req.func_cb = NULL;
53 val = twl4030_madc_conversion(&req);
54 if (val < 0)
55 return val;
56
57 return sprintf(buf, "%d\n", req.rbuf[attr->index]);
58}
59
60/* sysfs nodes to read individual channels from user side */
61static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, madc_read, NULL, 0);
62static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, madc_read, NULL, 1);
63static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, madc_read, NULL, 2);
64static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, madc_read, NULL, 3);
65static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, madc_read, NULL, 4);
66static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, madc_read, NULL, 5);
67static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, madc_read, NULL, 6);
68static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, madc_read, NULL, 7);
69static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, madc_read, NULL, 8);
70static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, madc_read, NULL, 9);
71static SENSOR_DEVICE_ATTR(curr10_input, S_IRUGO, madc_read, NULL, 10);
72static SENSOR_DEVICE_ATTR(in11_input, S_IRUGO, madc_read, NULL, 11);
73static SENSOR_DEVICE_ATTR(in12_input, S_IRUGO, madc_read, NULL, 12);
74static SENSOR_DEVICE_ATTR(in15_input, S_IRUGO, madc_read, NULL, 15);
75
76static struct attribute *twl4030_madc_attributes[] = {
77 &sensor_dev_attr_in0_input.dev_attr.attr,
78 &sensor_dev_attr_temp1_input.dev_attr.attr,
79 &sensor_dev_attr_in2_input.dev_attr.attr,
80 &sensor_dev_attr_in3_input.dev_attr.attr,
81 &sensor_dev_attr_in4_input.dev_attr.attr,
82 &sensor_dev_attr_in5_input.dev_attr.attr,
83 &sensor_dev_attr_in6_input.dev_attr.attr,
84 &sensor_dev_attr_in7_input.dev_attr.attr,
85 &sensor_dev_attr_in8_input.dev_attr.attr,
86 &sensor_dev_attr_in9_input.dev_attr.attr,
87 &sensor_dev_attr_curr10_input.dev_attr.attr,
88 &sensor_dev_attr_in11_input.dev_attr.attr,
89 &sensor_dev_attr_in12_input.dev_attr.attr,
90 &sensor_dev_attr_in15_input.dev_attr.attr,
91 NULL
92};
93
94static const struct attribute_group twl4030_madc_group = {
95 .attrs = twl4030_madc_attributes,
96};
97
98static int __devinit twl4030_madc_hwmon_probe(struct platform_device *pdev)
99{
100 int ret;
Keerthy0070bdd2011-03-01 19:12:36 +0530101 struct device *hwmon;
102
103 ret = sysfs_create_group(&pdev->dev.kobj, &twl4030_madc_group);
104 if (ret)
105 goto err_sysfs;
106 hwmon = hwmon_device_register(&pdev->dev);
107 if (IS_ERR(hwmon)) {
108 dev_err(&pdev->dev, "hwmon_device_register failed.\n");
Axel Linc30aa392011-05-01 11:34:55 -0400109 ret = PTR_ERR(hwmon);
Keerthy0070bdd2011-03-01 19:12:36 +0530110 goto err_reg;
111 }
112
113 return 0;
114
115err_reg:
116 sysfs_remove_group(&pdev->dev.kobj, &twl4030_madc_group);
117err_sysfs:
118
119 return ret;
120}
121
122static int __devexit twl4030_madc_hwmon_remove(struct platform_device *pdev)
123{
124 hwmon_device_unregister(&pdev->dev);
125 sysfs_remove_group(&pdev->dev.kobj, &twl4030_madc_group);
126
127 return 0;
128}
129
130static struct platform_driver twl4030_madc_hwmon_driver = {
131 .probe = twl4030_madc_hwmon_probe,
132 .remove = __exit_p(twl4030_madc_hwmon_remove),
133 .driver = {
134 .name = "twl4030_madc_hwmon",
135 .owner = THIS_MODULE,
136 },
137};
138
Axel Lin25a236a2011-11-25 02:31:00 -0500139module_platform_driver(twl4030_madc_hwmon_driver);
Keerthy0070bdd2011-03-01 19:12:36 +0530140
141MODULE_DESCRIPTION("TWL4030 ADC Hwmon driver");
142MODULE_LICENSE("GPL");
143MODULE_AUTHOR("J Keerthy");
Axel Linc4757ee2011-03-07 11:05:33 +0800144MODULE_ALIAS("platform:twl4030_madc_hwmon");