hwmon: (ltc4151) Fix multi-line comments
[linux-2.6-block.git] / drivers / hwmon / max1111.c
CommitLineData
55b951e7
EM
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.
91c8eabe 9 * Eric Miao <eric.miao@marvell.com>
55b951e7
EM
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>
5a0e3ad6 23#include <linux/slab.h>
55b951e7
EM
24
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];
d737c09c
JD
41 uint8_t tx_buf[MAX1111_TX_BUF_SIZE];
42 uint8_t rx_buf[MAX1111_RX_BUF_SIZE];
d3f684f2
PH
43 struct mutex drvdata_lock;
44 /* protect msg, xfer and buffers from multiple access */
55b951e7
EM
45};
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
d3f684f2
PH
53 /* writing to drvdata struct is not thread safe, wait on mutex */
54 mutex_lock(&data->drvdata_lock);
55
55b951e7
EM
56 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);
d3f684f2 63 mutex_unlock(&data->drvdata_lock);
55b951e7
EM
64 return err;
65 }
66
67 v1 = data->rx_buf[0];
68 v2 = data->rx_buf[1];
69
d3f684f2
PH
70 mutex_unlock(&data->drvdata_lock);
71
55b951e7
EM
72 if ((v1 & 0xc0) || (v2 & 0x3f))
73 return -EINVAL;
74
75 return (v1 << 2) | (v2 >> 6);
76}
77
f16177c2
EM
78#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
55b951e7
EM
88/*
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
012f3b91
EM
109 /* assume the reference voltage to be 2.048V, with an 8-bit sample,
110 * the LSB weight is 8mV
111 */
112 return sprintf(buf, "%d\n", ret * 8);
55b951e7
EM
113}
114
115#define MAX1111_ADC_ATTR(_id) \
012f3b91 116 SENSOR_DEVICE_ATTR(in##_id##_input, S_IRUGO, show_adc, NULL, _id)
55b951e7
EM
117
118static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
119static MAX1111_ADC_ATTR(0);
120static MAX1111_ADC_ATTR(1);
121static MAX1111_ADC_ATTR(2);
122static MAX1111_ADC_ATTR(3);
123
124static struct attribute *max1111_attributes[] = {
125 &dev_attr_name.attr,
012f3b91
EM
126 &sensor_dev_attr_in0_input.dev_attr.attr,
127 &sensor_dev_attr_in1_input.dev_attr.attr,
128 &sensor_dev_attr_in2_input.dev_attr.attr,
129 &sensor_dev_attr_in3_input.dev_attr.attr,
55b951e7
EM
130 NULL,
131};
132
133static const struct attribute_group max1111_attr_group = {
134 .attrs = max1111_attributes,
135};
136
d737c09c 137static int __devinit setup_transfer(struct max1111_data *data)
55b951e7
EM
138{
139 struct spi_message *m;
140 struct spi_transfer *x;
141
55b951e7
EM
142 m = &data->msg;
143 x = &data->xfer[0];
144
145 spi_message_init(m);
146
147 x->tx_buf = &data->tx_buf[0];
d737c09c 148 x->len = MAX1111_TX_BUF_SIZE;
55b951e7
EM
149 spi_message_add_tail(x, m);
150
151 x++;
152 x->rx_buf = &data->rx_buf[0];
d737c09c 153 x->len = MAX1111_RX_BUF_SIZE;
55b951e7
EM
154 spi_message_add_tail(x, m);
155
156 return 0;
157}
158
159static int __devinit max1111_probe(struct spi_device *spi)
160{
161 struct max1111_data *data;
162 int err;
163
164 spi->bits_per_word = 8;
165 spi->mode = SPI_MODE_0;
166 err = spi_setup(spi);
167 if (err < 0)
168 return err;
169
170 data = kzalloc(sizeof(struct max1111_data), GFP_KERNEL);
171 if (data == NULL) {
172 dev_err(&spi->dev, "failed to allocate memory\n");
173 return -ENOMEM;
174 }
175
176 err = setup_transfer(data);
177 if (err)
178 goto err_free_data;
179
d3f684f2
PH
180 mutex_init(&data->drvdata_lock);
181
55b951e7
EM
182 data->spi = spi;
183 spi_set_drvdata(spi, data);
184
185 err = sysfs_create_group(&spi->dev.kobj, &max1111_attr_group);
186 if (err) {
187 dev_err(&spi->dev, "failed to create attribute group\n");
d737c09c 188 goto err_free_data;
55b951e7
EM
189 }
190
191 data->hwmon_dev = hwmon_device_register(&spi->dev);
192 if (IS_ERR(data->hwmon_dev)) {
193 dev_err(&spi->dev, "failed to create hwmon device\n");
194 err = PTR_ERR(data->hwmon_dev);
195 goto err_remove;
196 }
197
f16177c2
EM
198#ifdef CONFIG_SHARPSL_PM
199 the_max1111 = data;
200#endif
55b951e7
EM
201 return 0;
202
203err_remove:
204 sysfs_remove_group(&spi->dev.kobj, &max1111_attr_group);
55b951e7
EM
205err_free_data:
206 kfree(data);
207 return err;
208}
209
210static int __devexit max1111_remove(struct spi_device *spi)
211{
212 struct max1111_data *data = spi_get_drvdata(spi);
213
214 hwmon_device_unregister(data->hwmon_dev);
215 sysfs_remove_group(&spi->dev.kobj, &max1111_attr_group);
d3f684f2 216 mutex_destroy(&data->drvdata_lock);
55b951e7
EM
217 kfree(data);
218 return 0;
219}
220
221static struct spi_driver max1111_driver = {
222 .driver = {
223 .name = "max1111",
224 .owner = THIS_MODULE,
225 },
226 .probe = max1111_probe,
227 .remove = __devexit_p(max1111_remove),
228};
229
91efffe2 230module_spi_driver(max1111_driver);
55b951e7
EM
231
232MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
233MODULE_DESCRIPTION("MAX1111 ADC Driver");
234MODULE_LICENSE("GPL");
e0626e38 235MODULE_ALIAS("spi:max1111");