libata: mask off DET when restoring SControl for detach
[linux-2.6/mini2440.git] / drivers / hwmon / max1111.c
blobbfaa665ccf324a25d9e40368135f24b876d0b8d6
1 /*
2 * max1111.c - +2.7V, Low-Power, Multichannel, Serial 8-bit ADCs
4 * Based on arch/arm/mach-pxa/corgi_ssp.c
6 * Copyright (C) 2004-2005 Richard Purdie
8 * Copyright (C) 2008 Marvell International Ltd.
9 * Eric Miao <eric.miao@marvell.com>
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.
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>
24 #define MAX1111_TX_BUF_SIZE 1
25 #define MAX1111_RX_BUF_SIZE 2
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)
35 struct 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;
44 static int max1111_read(struct device *dev, int channel)
46 struct max1111_data *data = dev_get_drvdata(dev);
47 uint8_t v1, v2;
48 int err;
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;
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;
60 v1 = data->rx_buf[0];
61 v2 = data->rx_buf[1];
63 if ((v1 & 0xc0) || (v2 & 0x3f))
64 return -EINVAL;
66 return (v1 << 2) | (v2 >> 6);
69 #ifdef CONFIG_SHARPSL_PM
70 static struct max1111_data *the_max1111;
72 int max1111_read_channel(int channel)
74 return max1111_read(&the_max1111->spi->dev, channel);
76 EXPORT_SYMBOL(max1111_read_channel);
77 #endif
80 * NOTE: SPI devices do not have a default 'name' attribute, which is
81 * likely to be used by hwmon applications to distinguish between
82 * different devices, explicitly add a name attribute here.
84 static ssize_t show_name(struct device *dev,
85 struct device_attribute *attr, char *buf)
87 return sprintf(buf, "max1111\n");
90 static ssize_t show_adc(struct device *dev,
91 struct device_attribute *attr, char *buf)
93 int channel = to_sensor_dev_attr(attr)->index;
94 int ret;
96 ret = max1111_read(dev, channel);
97 if (ret < 0)
98 return ret;
100 return sprintf(buf, "%d\n", ret);
103 #define MAX1111_ADC_ATTR(_id) \
104 SENSOR_DEVICE_ATTR(adc##_id##_in, S_IRUGO, show_adc, NULL, _id)
106 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
107 static MAX1111_ADC_ATTR(0);
108 static MAX1111_ADC_ATTR(1);
109 static MAX1111_ADC_ATTR(2);
110 static MAX1111_ADC_ATTR(3);
112 static struct attribute *max1111_attributes[] = {
113 &dev_attr_name.attr,
114 &sensor_dev_attr_adc0_in.dev_attr.attr,
115 &sensor_dev_attr_adc1_in.dev_attr.attr,
116 &sensor_dev_attr_adc2_in.dev_attr.attr,
117 &sensor_dev_attr_adc3_in.dev_attr.attr,
118 NULL,
121 static const struct attribute_group max1111_attr_group = {
122 .attrs = max1111_attributes,
125 static int setup_transfer(struct max1111_data *data)
127 struct spi_message *m;
128 struct spi_transfer *x;
130 data->tx_buf = kmalloc(MAX1111_TX_BUF_SIZE, GFP_KERNEL);
131 if (!data->tx_buf)
132 return -ENOMEM;
134 data->rx_buf = kmalloc(MAX1111_RX_BUF_SIZE, GFP_KERNEL);
135 if (!data->rx_buf) {
136 kfree(data->tx_buf);
137 return -ENOMEM;
140 m = &data->msg;
141 x = &data->xfer[0];
143 spi_message_init(m);
145 x->tx_buf = &data->tx_buf[0];
146 x->len = 1;
147 spi_message_add_tail(x, m);
149 x++;
150 x->rx_buf = &data->rx_buf[0];
151 x->len = 2;
152 spi_message_add_tail(x, m);
154 return 0;
157 static int __devinit max1111_probe(struct spi_device *spi)
159 struct max1111_data *data;
160 int err;
162 spi->bits_per_word = 8;
163 spi->mode = SPI_MODE_0;
164 err = spi_setup(spi);
165 if (err < 0)
166 return err;
168 data = kzalloc(sizeof(struct max1111_data), GFP_KERNEL);
169 if (data == NULL) {
170 dev_err(&spi->dev, "failed to allocate memory\n");
171 return -ENOMEM;
174 err = setup_transfer(data);
175 if (err)
176 goto err_free_data;
178 data->spi = spi;
179 spi_set_drvdata(spi, data);
181 err = sysfs_create_group(&spi->dev.kobj, &max1111_attr_group);
182 if (err) {
183 dev_err(&spi->dev, "failed to create attribute group\n");
184 goto err_free_all;
187 data->hwmon_dev = hwmon_device_register(&spi->dev);
188 if (IS_ERR(data->hwmon_dev)) {
189 dev_err(&spi->dev, "failed to create hwmon device\n");
190 err = PTR_ERR(data->hwmon_dev);
191 goto err_remove;
194 #ifdef CONFIG_SHARPSL_PM
195 the_max1111 = data;
196 #endif
197 return 0;
199 err_remove:
200 sysfs_remove_group(&spi->dev.kobj, &max1111_attr_group);
201 err_free_all:
202 kfree(data->rx_buf);
203 kfree(data->tx_buf);
204 err_free_data:
205 kfree(data);
206 return err;
209 static int __devexit max1111_remove(struct spi_device *spi)
211 struct max1111_data *data = spi_get_drvdata(spi);
213 hwmon_device_unregister(data->hwmon_dev);
214 sysfs_remove_group(&spi->dev.kobj, &max1111_attr_group);
215 kfree(data->rx_buf);
216 kfree(data->tx_buf);
217 kfree(data);
218 return 0;
221 static struct spi_driver max1111_driver = {
222 .driver = {
223 .name = "max1111",
224 .owner = THIS_MODULE,
226 .probe = max1111_probe,
227 .remove = __devexit_p(max1111_remove),
230 static int __init max1111_init(void)
232 return spi_register_driver(&max1111_driver);
234 module_init(max1111_init);
236 static void __exit max1111_exit(void)
238 spi_unregister_driver(&max1111_driver);
240 module_exit(max1111_exit);
242 MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
243 MODULE_DESCRIPTION("MAX1111 ADC Driver");
244 MODULE_LICENSE("GPL");