aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/hwmon/max1111.c
diff options
context:
space:
mode:
authorEric Miao <eric.miao@marvell.com>2008-08-28 16:26:48 -0400
committerRussell King <rmk+kernel@arm.linux.org.uk>2008-09-23 17:04:32 -0400
commit55b951e7e6b9f983286c40925e340124d79bb0f7 (patch)
treebf1169122419010c2d5ea1d6187e0fbcd280456d /drivers/hwmon/max1111.c
parentdd89ccb23a718a25dd989a27b04bf52871c9fb23 (diff)
hwmon: add max1111 Low-power Multichannel Serial 8-bit ADCs
Driver based on corgi_ssp.c and sharpsl_pm.c, previously done by Richard Purdie and many others. Now changed to generic HWMON device and expose all the ADC input value through sysfs. Signed-off-by: Eric Miao <eric.miao@marvell.com> Acked-by: Jean Delvare <khali@linux-fr.org> Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
Diffstat (limited to 'drivers/hwmon/max1111.c')
-rw-r--r--drivers/hwmon/max1111.c231
1 files changed, 231 insertions, 0 deletions
diff --git a/drivers/hwmon/max1111.c b/drivers/hwmon/max1111.c
new file mode 100644
index 000000000000..12c05774a1c3
--- /dev/null
+++ b/drivers/hwmon/max1111.c
@@ -0,0 +1,231 @@
1/*
2 * max1111.c - +2.7V, Low-Power, Multichannel, Serial 8-bit ADCs
3 *
4 * Based on arch/arm/mach-pxa/corgi_ssp.c
5 *
6 * Copyright (C) 2004-2005 Richard Purdie
7 *
8 * Copyright (C) 2008 Marvell International Ltd.
9 * Eric Miao <eric.miao@marvell.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * publishhed by the Free Software Foundation.
14 */
15
16#include <linux/module.h>
17#include <linux/kernel.h>
18#include <linux/init.h>
19#include <linux/err.h>
20#include <linux/hwmon.h>
21#include <linux/hwmon-sysfs.h>
22#include <linux/spi/spi.h>
23
24#define MAX1111_TX_BUF_SIZE 1
25#define MAX1111_RX_BUF_SIZE 2
26
27/* MAX1111 Commands */
28#define MAX1111_CTRL_PD0 (1u << 0)
29#define MAX1111_CTRL_PD1 (1u << 1)
30#define MAX1111_CTRL_SGL (1u << 2)
31#define MAX1111_CTRL_UNI (1u << 3)
32#define MAX1111_CTRL_SEL_SH (5) /* NOTE: bit 4 is ignored */
33#define MAX1111_CTRL_STR (1u << 7)
34
35struct max1111_data {
36 struct spi_device *spi;
37 struct device *hwmon_dev;
38 struct spi_message msg;
39 struct spi_transfer xfer[2];
40 uint8_t *tx_buf;
41 uint8_t *rx_buf;
42};
43
44static int max1111_read(struct device *dev, int channel)
45{
46 struct max1111_data *data = dev_get_drvdata(dev);
47 uint8_t v1, v2;
48 int err;
49
50 data->tx_buf[0] = (channel << MAX1111_CTRL_SEL_SH) |
51 MAX1111_CTRL_PD0 | MAX1111_CTRL_PD1 |
52 MAX1111_CTRL_SGL | MAX1111_CTRL_UNI | MAX1111_CTRL_STR;
53
54 err = spi_sync(data->spi, &data->msg);
55 if (err < 0) {
56 dev_err(dev, "spi_sync failed with %d\n", err);
57 return err;
58 }
59
60 v1 = data->rx_buf[0];
61 v2 = data->rx_buf[1];
62
63 if ((v1 & 0xc0) || (v2 & 0x3f))
64 return -EINVAL;
65
66 return (v1 << 2) | (v2 >> 6);
67}
68
69/*
70 * NOTE: SPI devices do not have a default 'name' attribute, which is
71 * likely to be used by hwmon applications to distinguish between
72 * different devices, explicitly add a name attribute here.
73 */
74static ssize_t show_name(struct device *dev,
75 struct device_attribute *attr, char *buf)
76{
77 return sprintf(buf, "max1111\n");
78}
79
80static ssize_t show_adc(struct device *dev,
81 struct device_attribute *attr, char *buf)
82{
83 int channel = to_sensor_dev_attr(attr)->index;
84 int ret;
85
86 ret = max1111_read(dev, channel);
87 if (ret < 0)
88 return ret;
89
90 return sprintf(buf, "%d\n", ret);
91}
92
93#define MAX1111_ADC_ATTR(_id) \
94 SENSOR_DEVICE_ATTR(adc##_id##_in, S_IRUGO, show_adc, NULL, _id)
95
96static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
97static MAX1111_ADC_ATTR(0);
98static MAX1111_ADC_ATTR(1);
99static MAX1111_ADC_ATTR(2);
100static MAX1111_ADC_ATTR(3);
101
102static struct attribute *max1111_attributes[] = {
103 &dev_attr_name.attr,
104 &sensor_dev_attr_adc0_in.dev_attr.attr,
105 &sensor_dev_attr_adc1_in.dev_attr.attr,
106 &sensor_dev_attr_adc2_in.dev_attr.attr,
107 &sensor_dev_attr_adc3_in.dev_attr.attr,
108 NULL,
109};
110
111static const struct attribute_group max1111_attr_group = {
112 .attrs = max1111_attributes,
113};
114
115static int setup_transfer(struct max1111_data *data)
116{
117 struct spi_message *m;
118 struct spi_transfer *x;
119
120 data->tx_buf = kmalloc(MAX1111_TX_BUF_SIZE, GFP_KERNEL);
121 if (!data->tx_buf)
122 return -ENOMEM;
123
124 data->rx_buf = kmalloc(MAX1111_RX_BUF_SIZE, GFP_KERNEL);
125 if (!data->rx_buf) {
126 kfree(data->tx_buf);
127 return -ENOMEM;
128 }
129
130 m = &data->msg;
131 x = &data->xfer[0];
132
133 spi_message_init(m);
134
135 x->tx_buf = &data->tx_buf[0];
136 x->len = 1;
137 spi_message_add_tail(x, m);
138
139 x++;
140 x->rx_buf = &data->rx_buf[0];
141 x->len = 2;
142 spi_message_add_tail(x, m);
143
144 return 0;
145}
146
147static int __devinit max1111_probe(struct spi_device *spi)
148{
149 struct max1111_data *data;
150 int err;
151
152 spi->bits_per_word = 8;
153 spi->mode = SPI_MODE_0;
154 err = spi_setup(spi);
155 if (err < 0)
156 return err;
157
158 data = kzalloc(sizeof(struct max1111_data), GFP_KERNEL);
159 if (data == NULL) {
160 dev_err(&spi->dev, "failed to allocate memory\n");
161 return -ENOMEM;
162 }
163
164 err = setup_transfer(data);
165 if (err)
166 goto err_free_data;
167
168 data->spi = spi;
169 spi_set_drvdata(spi, data);
170
171 err = sysfs_create_group(&spi->dev.kobj, &max1111_attr_group);
172 if (err) {
173 dev_err(&spi->dev, "failed to create attribute group\n");
174 goto err_free_all;
175 }
176
177 data->hwmon_dev = hwmon_device_register(&spi->dev);
178 if (IS_ERR(data->hwmon_dev)) {
179 dev_err(&spi->dev, "failed to create hwmon device\n");
180 err = PTR_ERR(data->hwmon_dev);
181 goto err_remove;
182 }
183
184 return 0;
185
186err_remove:
187 sysfs_remove_group(&spi->dev.kobj, &max1111_attr_group);
188err_free_all:
189 kfree(data->rx_buf);
190 kfree(data->tx_buf);
191err_free_data:
192 kfree(data);
193 return err;
194}
195
196static int __devexit max1111_remove(struct spi_device *spi)
197{
198 struct max1111_data *data = spi_get_drvdata(spi);
199
200 hwmon_device_unregister(data->hwmon_dev);
201 sysfs_remove_group(&spi->dev.kobj, &max1111_attr_group);
202 kfree(data->rx_buf);
203 kfree(data->tx_buf);
204 kfree(data);
205 return 0;
206}
207
208static struct spi_driver max1111_driver = {
209 .driver = {
210 .name = "max1111",
211 .owner = THIS_MODULE,
212 },
213 .probe = max1111_probe,
214 .remove = __devexit_p(max1111_remove),
215};
216
217static int __init max1111_init(void)
218{
219 return spi_register_driver(&max1111_driver);
220}
221module_init(max1111_init);
222
223static void __exit max1111_exit(void)
224{
225 spi_unregister_driver(&max1111_driver);
226}
227module_exit(max1111_exit);
228
229MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
230MODULE_DESCRIPTION("MAX1111 ADC Driver");
231MODULE_LICENSE("GPL");