blob: 362a40eb61299c5bd23ff2dde956d5683b4b4118 [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
25#define MAX1111_TX_BUF_SIZE 1
26#define MAX1111_RX_BUF_SIZE 2
27
28/* MAX1111 Commands */
29#define MAX1111_CTRL_PD0 (1u << 0)
30#define MAX1111_CTRL_PD1 (1u << 1)
31#define MAX1111_CTRL_SGL (1u << 2)
32#define MAX1111_CTRL_UNI (1u << 3)
33#define MAX1111_CTRL_SEL_SH (5) /* NOTE: bit 4 is ignored */
34#define MAX1111_CTRL_STR (1u << 7)
35
36struct max1111_data {
37 struct spi_device *spi;
38 struct device *hwmon_dev;
39 struct spi_message msg;
40 struct spi_transfer xfer[2];
Jean Delvared737c092011-07-25 21:46:10 +020041 uint8_t tx_buf[MAX1111_TX_BUF_SIZE];
42 uint8_t rx_buf[MAX1111_RX_BUF_SIZE];
Pavel Herrmannd3f684f2011-07-17 18:39:19 +020043 struct mutex drvdata_lock;
44 /* protect msg, xfer and buffers from multiple access */
Eric Miao55b951e2008-08-29 04:26:48 +080045};
46
47static int max1111_read(struct device *dev, int channel)
48{
49 struct max1111_data *data = dev_get_drvdata(dev);
50 uint8_t v1, v2;
51 int err;
52
Pavel Herrmannd3f684f2011-07-17 18:39:19 +020053 /* writing to drvdata struct is not thread safe, wait on mutex */
54 mutex_lock(&data->drvdata_lock);
55
Eric Miao55b951e2008-08-29 04:26:48 +080056 data->tx_buf[0] = (channel << MAX1111_CTRL_SEL_SH) |
57 MAX1111_CTRL_PD0 | MAX1111_CTRL_PD1 |
58 MAX1111_CTRL_SGL | MAX1111_CTRL_UNI | MAX1111_CTRL_STR;
59
60 err = spi_sync(data->spi, &data->msg);
61 if (err < 0) {
62 dev_err(dev, "spi_sync failed with %d\n", err);
Pavel Herrmannd3f684f2011-07-17 18:39:19 +020063 mutex_unlock(&data->drvdata_lock);
Eric Miao55b951e2008-08-29 04:26:48 +080064 return err;
65 }
66
67 v1 = data->rx_buf[0];
68 v2 = data->rx_buf[1];
69
Pavel Herrmannd3f684f2011-07-17 18:39:19 +020070 mutex_unlock(&data->drvdata_lock);
71
Eric Miao55b951e2008-08-29 04:26:48 +080072 if ((v1 & 0xc0) || (v2 & 0x3f))
73 return -EINVAL;
74
75 return (v1 << 2) | (v2 >> 6);
76}
77
Eric Miaof16177c2008-08-29 06:19:32 +080078#ifdef CONFIG_SHARPSL_PM
79static struct max1111_data *the_max1111;
80
81int max1111_read_channel(int channel)
82{
83 return max1111_read(&the_max1111->spi->dev, channel);
84}
85EXPORT_SYMBOL(max1111_read_channel);
86#endif
87
Eric Miao55b951e2008-08-29 04:26:48 +080088/*
89 * NOTE: SPI devices do not have a default 'name' attribute, which is
90 * likely to be used by hwmon applications to distinguish between
91 * different devices, explicitly add a name attribute here.
92 */
93static ssize_t show_name(struct device *dev,
94 struct device_attribute *attr, char *buf)
95{
96 return sprintf(buf, "max1111\n");
97}
98
99static ssize_t show_adc(struct device *dev,
100 struct device_attribute *attr, char *buf)
101{
102 int channel = to_sensor_dev_attr(attr)->index;
103 int ret;
104
105 ret = max1111_read(dev, channel);
106 if (ret < 0)
107 return ret;
108
Guenter Roeck51683ee2012-01-19 11:02:22 -0800109 /*
110 * assume the reference voltage to be 2.048V, with an 8-bit sample,
Eric Miao012f3b92012-01-16 22:51:45 +0100111 * the LSB weight is 8mV
112 */
113 return sprintf(buf, "%d\n", ret * 8);
Eric Miao55b951e2008-08-29 04:26:48 +0800114}
115
116#define MAX1111_ADC_ATTR(_id) \
Eric Miao012f3b92012-01-16 22:51:45 +0100117 SENSOR_DEVICE_ATTR(in##_id##_input, S_IRUGO, show_adc, NULL, _id)
Eric Miao55b951e2008-08-29 04:26:48 +0800118
119static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
120static MAX1111_ADC_ATTR(0);
121static MAX1111_ADC_ATTR(1);
122static MAX1111_ADC_ATTR(2);
123static MAX1111_ADC_ATTR(3);
124
125static struct attribute *max1111_attributes[] = {
126 &dev_attr_name.attr,
Eric Miao012f3b92012-01-16 22:51:45 +0100127 &sensor_dev_attr_in0_input.dev_attr.attr,
128 &sensor_dev_attr_in1_input.dev_attr.attr,
129 &sensor_dev_attr_in2_input.dev_attr.attr,
130 &sensor_dev_attr_in3_input.dev_attr.attr,
Eric Miao55b951e2008-08-29 04:26:48 +0800131 NULL,
132};
133
134static const struct attribute_group max1111_attr_group = {
135 .attrs = max1111_attributes,
136};
137
Jean Delvared737c092011-07-25 21:46:10 +0200138static int __devinit setup_transfer(struct max1111_data *data)
Eric Miao55b951e2008-08-29 04:26:48 +0800139{
140 struct spi_message *m;
141 struct spi_transfer *x;
142
Eric Miao55b951e2008-08-29 04:26:48 +0800143 m = &data->msg;
144 x = &data->xfer[0];
145
146 spi_message_init(m);
147
148 x->tx_buf = &data->tx_buf[0];
Jean Delvared737c092011-07-25 21:46:10 +0200149 x->len = MAX1111_TX_BUF_SIZE;
Eric Miao55b951e2008-08-29 04:26:48 +0800150 spi_message_add_tail(x, m);
151
152 x++;
153 x->rx_buf = &data->rx_buf[0];
Jean Delvared737c092011-07-25 21:46:10 +0200154 x->len = MAX1111_RX_BUF_SIZE;
Eric Miao55b951e2008-08-29 04:26:48 +0800155 spi_message_add_tail(x, m);
156
157 return 0;
158}
159
160static int __devinit max1111_probe(struct spi_device *spi)
161{
162 struct max1111_data *data;
163 int err;
164
165 spi->bits_per_word = 8;
166 spi->mode = SPI_MODE_0;
167 err = spi_setup(spi);
168 if (err < 0)
169 return err;
170
171 data = kzalloc(sizeof(struct max1111_data), GFP_KERNEL);
172 if (data == NULL) {
173 dev_err(&spi->dev, "failed to allocate memory\n");
174 return -ENOMEM;
175 }
176
177 err = setup_transfer(data);
178 if (err)
179 goto err_free_data;
180
Pavel Herrmannd3f684f2011-07-17 18:39:19 +0200181 mutex_init(&data->drvdata_lock);
182
Eric Miao55b951e2008-08-29 04:26:48 +0800183 data->spi = spi;
184 spi_set_drvdata(spi, data);
185
186 err = sysfs_create_group(&spi->dev.kobj, &max1111_attr_group);
187 if (err) {
188 dev_err(&spi->dev, "failed to create attribute group\n");
Jean Delvared737c092011-07-25 21:46:10 +0200189 goto err_free_data;
Eric Miao55b951e2008-08-29 04:26:48 +0800190 }
191
192 data->hwmon_dev = hwmon_device_register(&spi->dev);
193 if (IS_ERR(data->hwmon_dev)) {
194 dev_err(&spi->dev, "failed to create hwmon device\n");
195 err = PTR_ERR(data->hwmon_dev);
196 goto err_remove;
197 }
198
Eric Miaof16177c2008-08-29 06:19:32 +0800199#ifdef CONFIG_SHARPSL_PM
200 the_max1111 = data;
201#endif
Eric Miao55b951e2008-08-29 04:26:48 +0800202 return 0;
203
204err_remove:
205 sysfs_remove_group(&spi->dev.kobj, &max1111_attr_group);
Eric Miao55b951e2008-08-29 04:26:48 +0800206err_free_data:
207 kfree(data);
208 return err;
209}
210
211static int __devexit max1111_remove(struct spi_device *spi)
212{
213 struct max1111_data *data = spi_get_drvdata(spi);
214
215 hwmon_device_unregister(data->hwmon_dev);
216 sysfs_remove_group(&spi->dev.kobj, &max1111_attr_group);
Pavel Herrmannd3f684f2011-07-17 18:39:19 +0200217 mutex_destroy(&data->drvdata_lock);
Eric Miao55b951e2008-08-29 04:26:48 +0800218 kfree(data);
219 return 0;
220}
221
222static struct spi_driver max1111_driver = {
223 .driver = {
224 .name = "max1111",
225 .owner = THIS_MODULE,
226 },
227 .probe = max1111_probe,
228 .remove = __devexit_p(max1111_remove),
229};
230
Axel Lin91efffe2012-01-20 15:53:47 +0800231module_spi_driver(max1111_driver);
Eric Miao55b951e2008-08-29 04:26:48 +0800232
233MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
234MODULE_DESCRIPTION("MAX1111 ADC Driver");
235MODULE_LICENSE("GPL");
Anton Vorontsove0626e32009-09-22 16:46:08 -0700236MODULE_ALIAS("spi:max1111");