blob: eda077de8a9f726dd956f48e7e909fcc6ea8f148 [file] [log] [blame]
Eric Miao55b951e2008-08-29 04:26:48 +08001/*
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.
Frans Meulenbroeks91c8eab2012-01-04 23:27:19 +01009 * Eric Miao <eric.miao@marvell.com>
Eric Miao55b951e2008-08-29 04:26:48 +080010 *
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Eric Miao55b951e2008-08-29 04:26:48 +080024
Guenter Roeck9224c382012-08-20 18:52:36 -070025enum chips { max1110, max1111, max1112, max1113 };
26
Eric Miao55b951e2008-08-29 04:26:48 +080027#define MAX1111_TX_BUF_SIZE 1
28#define MAX1111_RX_BUF_SIZE 2
29
30/* MAX1111 Commands */
31#define MAX1111_CTRL_PD0 (1u << 0)
32#define MAX1111_CTRL_PD1 (1u << 1)
33#define MAX1111_CTRL_SGL (1u << 2)
34#define MAX1111_CTRL_UNI (1u << 3)
Guenter Roeck9224c382012-08-20 18:52:36 -070035#define MAX1110_CTRL_SEL_SH (4)
Eric Miao55b951e2008-08-29 04:26:48 +080036#define MAX1111_CTRL_SEL_SH (5) /* NOTE: bit 4 is ignored */
37#define MAX1111_CTRL_STR (1u << 7)
38
39struct max1111_data {
40 struct spi_device *spi;
41 struct device *hwmon_dev;
42 struct spi_message msg;
43 struct spi_transfer xfer[2];
Jean Delvared737c092011-07-25 21:46:10 +020044 uint8_t tx_buf[MAX1111_TX_BUF_SIZE];
45 uint8_t rx_buf[MAX1111_RX_BUF_SIZE];
Pavel Herrmannd3f684f2011-07-17 18:39:19 +020046 struct mutex drvdata_lock;
47 /* protect msg, xfer and buffers from multiple access */
Guenter Roeck9224c382012-08-20 18:52:36 -070048 int sel_sh;
49 int lsb;
Eric Miao55b951e2008-08-29 04:26:48 +080050};
51
52static int max1111_read(struct device *dev, int channel)
53{
54 struct max1111_data *data = dev_get_drvdata(dev);
55 uint8_t v1, v2;
56 int err;
57
Pavel Herrmannd3f684f2011-07-17 18:39:19 +020058 /* writing to drvdata struct is not thread safe, wait on mutex */
59 mutex_lock(&data->drvdata_lock);
60
Guenter Roeck9224c382012-08-20 18:52:36 -070061 data->tx_buf[0] = (channel << data->sel_sh) |
Eric Miao55b951e2008-08-29 04:26:48 +080062 MAX1111_CTRL_PD0 | MAX1111_CTRL_PD1 |
63 MAX1111_CTRL_SGL | MAX1111_CTRL_UNI | MAX1111_CTRL_STR;
64
65 err = spi_sync(data->spi, &data->msg);
66 if (err < 0) {
67 dev_err(dev, "spi_sync failed with %d\n", err);
Pavel Herrmannd3f684f2011-07-17 18:39:19 +020068 mutex_unlock(&data->drvdata_lock);
Eric Miao55b951e2008-08-29 04:26:48 +080069 return err;
70 }
71
72 v1 = data->rx_buf[0];
73 v2 = data->rx_buf[1];
74
Pavel Herrmannd3f684f2011-07-17 18:39:19 +020075 mutex_unlock(&data->drvdata_lock);
76
Eric Miao55b951e2008-08-29 04:26:48 +080077 if ((v1 & 0xc0) || (v2 & 0x3f))
78 return -EINVAL;
79
80 return (v1 << 2) | (v2 >> 6);
81}
82
Eric Miaof16177c2008-08-29 06:19:32 +080083#ifdef CONFIG_SHARPSL_PM
84static struct max1111_data *the_max1111;
85
86int max1111_read_channel(int channel)
87{
88 return max1111_read(&the_max1111->spi->dev, channel);
89}
90EXPORT_SYMBOL(max1111_read_channel);
91#endif
92
Eric Miao55b951e2008-08-29 04:26:48 +080093/*
94 * NOTE: SPI devices do not have a default 'name' attribute, which is
95 * likely to be used by hwmon applications to distinguish between
96 * different devices, explicitly add a name attribute here.
97 */
98static ssize_t show_name(struct device *dev,
99 struct device_attribute *attr, char *buf)
100{
Guenter Roeck9224c382012-08-20 18:52:36 -0700101 return sprintf(buf, "%s\n", to_spi_device(dev)->modalias);
Eric Miao55b951e2008-08-29 04:26:48 +0800102}
103
104static ssize_t show_adc(struct device *dev,
105 struct device_attribute *attr, char *buf)
106{
Guenter Roeck9224c382012-08-20 18:52:36 -0700107 struct max1111_data *data = dev_get_drvdata(dev);
Eric Miao55b951e2008-08-29 04:26:48 +0800108 int channel = to_sensor_dev_attr(attr)->index;
109 int ret;
110
111 ret = max1111_read(dev, channel);
112 if (ret < 0)
113 return ret;
114
Guenter Roeck51683ee2012-01-19 11:02:22 -0800115 /*
Guenter Roeck9224c382012-08-20 18:52:36 -0700116 * Assume the reference voltage to be 2.048V or 4.096V, with an 8-bit
117 * sample. The LSB weight is 8mV or 16mV depending on the chip type.
Eric Miao012f3b92012-01-16 22:51:45 +0100118 */
Guenter Roeck9224c382012-08-20 18:52:36 -0700119 return sprintf(buf, "%d\n", ret * data->lsb);
Eric Miao55b951e2008-08-29 04:26:48 +0800120}
121
122#define MAX1111_ADC_ATTR(_id) \
Eric Miao012f3b92012-01-16 22:51:45 +0100123 SENSOR_DEVICE_ATTR(in##_id##_input, S_IRUGO, show_adc, NULL, _id)
Eric Miao55b951e2008-08-29 04:26:48 +0800124
125static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
126static MAX1111_ADC_ATTR(0);
127static MAX1111_ADC_ATTR(1);
128static MAX1111_ADC_ATTR(2);
129static MAX1111_ADC_ATTR(3);
Guenter Roeck9224c382012-08-20 18:52:36 -0700130static MAX1111_ADC_ATTR(4);
131static MAX1111_ADC_ATTR(5);
132static MAX1111_ADC_ATTR(6);
133static MAX1111_ADC_ATTR(7);
Eric Miao55b951e2008-08-29 04:26:48 +0800134
135static struct attribute *max1111_attributes[] = {
136 &dev_attr_name.attr,
Eric Miao012f3b92012-01-16 22:51:45 +0100137 &sensor_dev_attr_in0_input.dev_attr.attr,
138 &sensor_dev_attr_in1_input.dev_attr.attr,
139 &sensor_dev_attr_in2_input.dev_attr.attr,
140 &sensor_dev_attr_in3_input.dev_attr.attr,
Eric Miao55b951e2008-08-29 04:26:48 +0800141 NULL,
142};
143
144static const struct attribute_group max1111_attr_group = {
145 .attrs = max1111_attributes,
146};
147
Guenter Roeck9224c382012-08-20 18:52:36 -0700148static struct attribute *max1110_attributes[] = {
149 &sensor_dev_attr_in4_input.dev_attr.attr,
150 &sensor_dev_attr_in5_input.dev_attr.attr,
151 &sensor_dev_attr_in6_input.dev_attr.attr,
152 &sensor_dev_attr_in7_input.dev_attr.attr,
153 NULL,
154};
155
156static const struct attribute_group max1110_attr_group = {
157 .attrs = max1110_attributes,
158};
159
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500160static int setup_transfer(struct max1111_data *data)
Eric Miao55b951e2008-08-29 04:26:48 +0800161{
162 struct spi_message *m;
163 struct spi_transfer *x;
164
Eric Miao55b951e2008-08-29 04:26:48 +0800165 m = &data->msg;
166 x = &data->xfer[0];
167
168 spi_message_init(m);
169
170 x->tx_buf = &data->tx_buf[0];
Jean Delvared737c092011-07-25 21:46:10 +0200171 x->len = MAX1111_TX_BUF_SIZE;
Eric Miao55b951e2008-08-29 04:26:48 +0800172 spi_message_add_tail(x, m);
173
174 x++;
175 x->rx_buf = &data->rx_buf[0];
Jean Delvared737c092011-07-25 21:46:10 +0200176 x->len = MAX1111_RX_BUF_SIZE;
Eric Miao55b951e2008-08-29 04:26:48 +0800177 spi_message_add_tail(x, m);
178
179 return 0;
180}
181
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500182static int max1111_probe(struct spi_device *spi)
Eric Miao55b951e2008-08-29 04:26:48 +0800183{
Guenter Roeck9224c382012-08-20 18:52:36 -0700184 enum chips chip = spi_get_device_id(spi)->driver_data;
Eric Miao55b951e2008-08-29 04:26:48 +0800185 struct max1111_data *data;
186 int err;
187
188 spi->bits_per_word = 8;
189 spi->mode = SPI_MODE_0;
190 err = spi_setup(spi);
191 if (err < 0)
192 return err;
193
Guenter Roeck661f0202012-06-02 09:58:12 -0700194 data = devm_kzalloc(&spi->dev, sizeof(struct max1111_data), GFP_KERNEL);
Eric Miao55b951e2008-08-29 04:26:48 +0800195 if (data == NULL) {
196 dev_err(&spi->dev, "failed to allocate memory\n");
197 return -ENOMEM;
198 }
199
Guenter Roeck9224c382012-08-20 18:52:36 -0700200 switch (chip) {
201 case max1110:
202 data->lsb = 8;
203 data->sel_sh = MAX1110_CTRL_SEL_SH;
204 break;
205 case max1111:
206 data->lsb = 8;
207 data->sel_sh = MAX1111_CTRL_SEL_SH;
208 break;
209 case max1112:
210 data->lsb = 16;
211 data->sel_sh = MAX1110_CTRL_SEL_SH;
212 break;
213 case max1113:
214 data->lsb = 16;
215 data->sel_sh = MAX1111_CTRL_SEL_SH;
216 break;
217 }
Eric Miao55b951e2008-08-29 04:26:48 +0800218 err = setup_transfer(data);
219 if (err)
Guenter Roeck661f0202012-06-02 09:58:12 -0700220 return err;
Eric Miao55b951e2008-08-29 04:26:48 +0800221
Pavel Herrmannd3f684f2011-07-17 18:39:19 +0200222 mutex_init(&data->drvdata_lock);
223
Eric Miao55b951e2008-08-29 04:26:48 +0800224 data->spi = spi;
225 spi_set_drvdata(spi, data);
226
227 err = sysfs_create_group(&spi->dev.kobj, &max1111_attr_group);
228 if (err) {
229 dev_err(&spi->dev, "failed to create attribute group\n");
Guenter Roeck661f0202012-06-02 09:58:12 -0700230 return err;
Eric Miao55b951e2008-08-29 04:26:48 +0800231 }
Guenter Roeck9224c382012-08-20 18:52:36 -0700232 if (chip == max1110 || chip == max1112) {
233 err = sysfs_create_group(&spi->dev.kobj, &max1110_attr_group);
234 if (err) {
235 dev_err(&spi->dev,
236 "failed to create extended attribute group\n");
237 goto err_remove;
238 }
239 }
Eric Miao55b951e2008-08-29 04:26:48 +0800240
241 data->hwmon_dev = hwmon_device_register(&spi->dev);
242 if (IS_ERR(data->hwmon_dev)) {
243 dev_err(&spi->dev, "failed to create hwmon device\n");
244 err = PTR_ERR(data->hwmon_dev);
245 goto err_remove;
246 }
247
Eric Miaof16177c2008-08-29 06:19:32 +0800248#ifdef CONFIG_SHARPSL_PM
249 the_max1111 = data;
250#endif
Eric Miao55b951e2008-08-29 04:26:48 +0800251 return 0;
252
253err_remove:
Guenter Roeck9224c382012-08-20 18:52:36 -0700254 sysfs_remove_group(&spi->dev.kobj, &max1110_attr_group);
Eric Miao55b951e2008-08-29 04:26:48 +0800255 sysfs_remove_group(&spi->dev.kobj, &max1111_attr_group);
Eric Miao55b951e2008-08-29 04:26:48 +0800256 return err;
257}
258
Bill Pemberton281dfd02012-11-19 13:25:51 -0500259static int max1111_remove(struct spi_device *spi)
Eric Miao55b951e2008-08-29 04:26:48 +0800260{
261 struct max1111_data *data = spi_get_drvdata(spi);
262
263 hwmon_device_unregister(data->hwmon_dev);
Guenter Roeck9224c382012-08-20 18:52:36 -0700264 sysfs_remove_group(&spi->dev.kobj, &max1110_attr_group);
Eric Miao55b951e2008-08-29 04:26:48 +0800265 sysfs_remove_group(&spi->dev.kobj, &max1111_attr_group);
Pavel Herrmannd3f684f2011-07-17 18:39:19 +0200266 mutex_destroy(&data->drvdata_lock);
Eric Miao55b951e2008-08-29 04:26:48 +0800267 return 0;
268}
269
Guenter Roeck9224c382012-08-20 18:52:36 -0700270static const struct spi_device_id max1111_ids[] = {
271 { "max1110", max1110 },
272 { "max1111", max1111 },
273 { "max1112", max1112 },
274 { "max1113", max1113 },
275 { },
276};
277MODULE_DEVICE_TABLE(spi, max1111_ids);
278
Eric Miao55b951e2008-08-29 04:26:48 +0800279static struct spi_driver max1111_driver = {
280 .driver = {
281 .name = "max1111",
282 .owner = THIS_MODULE,
283 },
Guenter Roeck9224c382012-08-20 18:52:36 -0700284 .id_table = max1111_ids,
Eric Miao55b951e2008-08-29 04:26:48 +0800285 .probe = max1111_probe,
Bill Pemberton9e5e9b72012-11-19 13:21:20 -0500286 .remove = max1111_remove,
Eric Miao55b951e2008-08-29 04:26:48 +0800287};
288
Axel Lin91efffe2012-01-20 15:53:47 +0800289module_spi_driver(max1111_driver);
Eric Miao55b951e2008-08-29 04:26:48 +0800290
291MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
Guenter Roeck9224c382012-08-20 18:52:36 -0700292MODULE_DESCRIPTION("MAX1110/MAX1111/MAX1112/MAX1113 ADC Driver");
Eric Miao55b951e2008-08-29 04:26:48 +0800293MODULE_LICENSE("GPL");