Loading...
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#include <linux/slab.h>
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];
41 uint8_t tx_buf[MAX1111_TX_BUF_SIZE];
42 uint8_t rx_buf[MAX1111_RX_BUF_SIZE];
43 struct mutex drvdata_lock;
44 /* protect msg, xfer and buffers from multiple access */
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
53 /* writing to drvdata struct is not thread safe, wait on mutex */
54 mutex_lock(&data->drvdata_lock);
55
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);
63 mutex_unlock(&data->drvdata_lock);
64 return err;
65 }
66
67 v1 = data->rx_buf[0];
68 v2 = data->rx_buf[1];
69
70 mutex_unlock(&data->drvdata_lock);
71
72 if ((v1 & 0xc0) || (v2 & 0x3f))
73 return -EINVAL;
74
75 return (v1 << 2) | (v2 >> 6);
76}
77
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
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
109 return sprintf(buf, "%d\n", ret);
110}
111
112#define MAX1111_ADC_ATTR(_id) \
113 SENSOR_DEVICE_ATTR(adc##_id##_in, S_IRUGO, show_adc, NULL, _id)
114
115static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
116static MAX1111_ADC_ATTR(0);
117static MAX1111_ADC_ATTR(1);
118static MAX1111_ADC_ATTR(2);
119static MAX1111_ADC_ATTR(3);
120
121static struct attribute *max1111_attributes[] = {
122 &dev_attr_name.attr,
123 &sensor_dev_attr_adc0_in.dev_attr.attr,
124 &sensor_dev_attr_adc1_in.dev_attr.attr,
125 &sensor_dev_attr_adc2_in.dev_attr.attr,
126 &sensor_dev_attr_adc3_in.dev_attr.attr,
127 NULL,
128};
129
130static const struct attribute_group max1111_attr_group = {
131 .attrs = max1111_attributes,
132};
133
134static int __devinit setup_transfer(struct max1111_data *data)
135{
136 struct spi_message *m;
137 struct spi_transfer *x;
138
139 m = &data->msg;
140 x = &data->xfer[0];
141
142 spi_message_init(m);
143
144 x->tx_buf = &data->tx_buf[0];
145 x->len = MAX1111_TX_BUF_SIZE;
146 spi_message_add_tail(x, m);
147
148 x++;
149 x->rx_buf = &data->rx_buf[0];
150 x->len = MAX1111_RX_BUF_SIZE;
151 spi_message_add_tail(x, m);
152
153 return 0;
154}
155
156static int __devinit max1111_probe(struct spi_device *spi)
157{
158 struct max1111_data *data;
159 int err;
160
161 spi->bits_per_word = 8;
162 spi->mode = SPI_MODE_0;
163 err = spi_setup(spi);
164 if (err < 0)
165 return err;
166
167 data = kzalloc(sizeof(struct max1111_data), GFP_KERNEL);
168 if (data == NULL) {
169 dev_err(&spi->dev, "failed to allocate memory\n");
170 return -ENOMEM;
171 }
172
173 err = setup_transfer(data);
174 if (err)
175 goto err_free_data;
176
177 mutex_init(&data->drvdata_lock);
178
179 data->spi = spi;
180 spi_set_drvdata(spi, data);
181
182 err = sysfs_create_group(&spi->dev.kobj, &max1111_attr_group);
183 if (err) {
184 dev_err(&spi->dev, "failed to create attribute group\n");
185 goto err_free_data;
186 }
187
188 data->hwmon_dev = hwmon_device_register(&spi->dev);
189 if (IS_ERR(data->hwmon_dev)) {
190 dev_err(&spi->dev, "failed to create hwmon device\n");
191 err = PTR_ERR(data->hwmon_dev);
192 goto err_remove;
193 }
194
195#ifdef CONFIG_SHARPSL_PM
196 the_max1111 = data;
197#endif
198 return 0;
199
200err_remove:
201 sysfs_remove_group(&spi->dev.kobj, &max1111_attr_group);
202err_free_data:
203 kfree(data);
204 return err;
205}
206
207static int __devexit max1111_remove(struct spi_device *spi)
208{
209 struct max1111_data *data = spi_get_drvdata(spi);
210
211 hwmon_device_unregister(data->hwmon_dev);
212 sysfs_remove_group(&spi->dev.kobj, &max1111_attr_group);
213 mutex_destroy(&data->drvdata_lock);
214 kfree(data);
215 return 0;
216}
217
218static struct spi_driver max1111_driver = {
219 .driver = {
220 .name = "max1111",
221 .owner = THIS_MODULE,
222 },
223 .probe = max1111_probe,
224 .remove = __devexit_p(max1111_remove),
225};
226
227static int __init max1111_init(void)
228{
229 return spi_register_driver(&max1111_driver);
230}
231module_init(max1111_init);
232
233static void __exit max1111_exit(void)
234{
235 spi_unregister_driver(&max1111_driver);
236}
237module_exit(max1111_exit);
238
239MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
240MODULE_DESCRIPTION("MAX1111 ADC Driver");
241MODULE_LICENSE("GPL");
242MODULE_ALIAS("spi:max1111");
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * max1111.c - +2.7V, Low-Power, Multichannel, Serial 8-bit ADCs
4 *
5 * Based on arch/arm/mach-pxa/corgi_ssp.c
6 *
7 * Copyright (C) 2004-2005 Richard Purdie
8 *
9 * Copyright (C) 2008 Marvell International Ltd.
10 * Eric Miao <eric.miao@marvell.com>
11 */
12
13#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/init.h>
16#include <linux/err.h>
17#include <linux/hwmon.h>
18#include <linux/hwmon-sysfs.h>
19#include <linux/spi/spi.h>
20#include <linux/slab.h>
21
22enum chips { max1110, max1111, max1112, max1113 };
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 MAX1110_CTRL_SEL_SH (4)
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];
41 uint8_t tx_buf[MAX1111_TX_BUF_SIZE];
42 uint8_t rx_buf[MAX1111_RX_BUF_SIZE];
43 struct mutex drvdata_lock;
44 /* protect msg, xfer and buffers from multiple access */
45 int sel_sh;
46 int lsb;
47};
48
49static int max1111_read(struct device *dev, int channel)
50{
51 struct max1111_data *data = dev_get_drvdata(dev);
52 uint8_t v1, v2;
53 int err;
54
55 /* writing to drvdata struct is not thread safe, wait on mutex */
56 mutex_lock(&data->drvdata_lock);
57
58 data->tx_buf[0] = (channel << data->sel_sh) |
59 MAX1111_CTRL_PD0 | MAX1111_CTRL_PD1 |
60 MAX1111_CTRL_SGL | MAX1111_CTRL_UNI | MAX1111_CTRL_STR;
61
62 err = spi_sync(data->spi, &data->msg);
63 if (err < 0) {
64 dev_err(dev, "spi_sync failed with %d\n", err);
65 mutex_unlock(&data->drvdata_lock);
66 return err;
67 }
68
69 v1 = data->rx_buf[0];
70 v2 = data->rx_buf[1];
71
72 mutex_unlock(&data->drvdata_lock);
73
74 if ((v1 & 0xc0) || (v2 & 0x3f))
75 return -EINVAL;
76
77 return (v1 << 2) | (v2 >> 6);
78}
79
80#ifdef CONFIG_SHARPSL_PM
81static struct max1111_data *the_max1111;
82
83int max1111_read_channel(int channel);
84int max1111_read_channel(int channel)
85{
86 if (!the_max1111 || !the_max1111->spi)
87 return -ENODEV;
88
89 return max1111_read(&the_max1111->spi->dev, channel);
90}
91EXPORT_SYMBOL(max1111_read_channel);
92#endif
93
94/*
95 * NOTE: SPI devices do not have a default 'name' attribute, which is
96 * likely to be used by hwmon applications to distinguish between
97 * different devices, explicitly add a name attribute here.
98 */
99static ssize_t name_show(struct device *dev,
100 struct device_attribute *attr, char *buf)
101{
102 return sprintf(buf, "%s\n", to_spi_device(dev)->modalias);
103}
104
105static ssize_t show_adc(struct device *dev,
106 struct device_attribute *attr, char *buf)
107{
108 struct max1111_data *data = dev_get_drvdata(dev);
109 int channel = to_sensor_dev_attr(attr)->index;
110 int ret;
111
112 ret = max1111_read(dev, channel);
113 if (ret < 0)
114 return ret;
115
116 /*
117 * Assume the reference voltage to be 2.048V or 4.096V, with an 8-bit
118 * sample. The LSB weight is 8mV or 16mV depending on the chip type.
119 */
120 return sprintf(buf, "%d\n", ret * data->lsb);
121}
122
123#define MAX1111_ADC_ATTR(_id) \
124 SENSOR_DEVICE_ATTR(in##_id##_input, S_IRUGO, show_adc, NULL, _id)
125
126static DEVICE_ATTR_RO(name);
127static MAX1111_ADC_ATTR(0);
128static MAX1111_ADC_ATTR(1);
129static MAX1111_ADC_ATTR(2);
130static MAX1111_ADC_ATTR(3);
131static MAX1111_ADC_ATTR(4);
132static MAX1111_ADC_ATTR(5);
133static MAX1111_ADC_ATTR(6);
134static MAX1111_ADC_ATTR(7);
135
136static struct attribute *max1111_attributes[] = {
137 &dev_attr_name.attr,
138 &sensor_dev_attr_in0_input.dev_attr.attr,
139 &sensor_dev_attr_in1_input.dev_attr.attr,
140 &sensor_dev_attr_in2_input.dev_attr.attr,
141 &sensor_dev_attr_in3_input.dev_attr.attr,
142 NULL,
143};
144
145static const struct attribute_group max1111_attr_group = {
146 .attrs = max1111_attributes,
147};
148
149static struct attribute *max1110_attributes[] = {
150 &sensor_dev_attr_in4_input.dev_attr.attr,
151 &sensor_dev_attr_in5_input.dev_attr.attr,
152 &sensor_dev_attr_in6_input.dev_attr.attr,
153 &sensor_dev_attr_in7_input.dev_attr.attr,
154 NULL,
155};
156
157static const struct attribute_group max1110_attr_group = {
158 .attrs = max1110_attributes,
159};
160
161static int setup_transfer(struct max1111_data *data)
162{
163 struct spi_message *m;
164 struct spi_transfer *x;
165
166 m = &data->msg;
167 x = &data->xfer[0];
168
169 spi_message_init(m);
170
171 x->tx_buf = &data->tx_buf[0];
172 x->len = MAX1111_TX_BUF_SIZE;
173 spi_message_add_tail(x, m);
174
175 x++;
176 x->rx_buf = &data->rx_buf[0];
177 x->len = MAX1111_RX_BUF_SIZE;
178 spi_message_add_tail(x, m);
179
180 return 0;
181}
182
183static int max1111_probe(struct spi_device *spi)
184{
185 enum chips chip = spi_get_device_id(spi)->driver_data;
186 struct max1111_data *data;
187 int err;
188
189 spi->bits_per_word = 8;
190 spi->mode = SPI_MODE_0;
191 err = spi_setup(spi);
192 if (err < 0)
193 return err;
194
195 data = devm_kzalloc(&spi->dev, sizeof(struct max1111_data), GFP_KERNEL);
196 if (data == NULL)
197 return -ENOMEM;
198
199 switch (chip) {
200 case max1110:
201 data->lsb = 8;
202 data->sel_sh = MAX1110_CTRL_SEL_SH;
203 break;
204 case max1111:
205 data->lsb = 8;
206 data->sel_sh = MAX1111_CTRL_SEL_SH;
207 break;
208 case max1112:
209 data->lsb = 16;
210 data->sel_sh = MAX1110_CTRL_SEL_SH;
211 break;
212 case max1113:
213 data->lsb = 16;
214 data->sel_sh = MAX1111_CTRL_SEL_SH;
215 break;
216 }
217 err = setup_transfer(data);
218 if (err)
219 return err;
220
221 mutex_init(&data->drvdata_lock);
222
223 data->spi = spi;
224 spi_set_drvdata(spi, data);
225
226 err = sysfs_create_group(&spi->dev.kobj, &max1111_attr_group);
227 if (err) {
228 dev_err(&spi->dev, "failed to create attribute group\n");
229 return err;
230 }
231 if (chip == max1110 || chip == max1112) {
232 err = sysfs_create_group(&spi->dev.kobj, &max1110_attr_group);
233 if (err) {
234 dev_err(&spi->dev,
235 "failed to create extended attribute group\n");
236 goto err_remove;
237 }
238 }
239
240 data->hwmon_dev = hwmon_device_register(&spi->dev);
241 if (IS_ERR(data->hwmon_dev)) {
242 dev_err(&spi->dev, "failed to create hwmon device\n");
243 err = PTR_ERR(data->hwmon_dev);
244 goto err_remove;
245 }
246
247#ifdef CONFIG_SHARPSL_PM
248 the_max1111 = data;
249#endif
250 return 0;
251
252err_remove:
253 sysfs_remove_group(&spi->dev.kobj, &max1110_attr_group);
254 sysfs_remove_group(&spi->dev.kobj, &max1111_attr_group);
255 return err;
256}
257
258static void max1111_remove(struct spi_device *spi)
259{
260 struct max1111_data *data = spi_get_drvdata(spi);
261
262#ifdef CONFIG_SHARPSL_PM
263 the_max1111 = NULL;
264#endif
265 hwmon_device_unregister(data->hwmon_dev);
266 sysfs_remove_group(&spi->dev.kobj, &max1110_attr_group);
267 sysfs_remove_group(&spi->dev.kobj, &max1111_attr_group);
268 mutex_destroy(&data->drvdata_lock);
269}
270
271static const struct spi_device_id max1111_ids[] = {
272 { "max1110", max1110 },
273 { "max1111", max1111 },
274 { "max1112", max1112 },
275 { "max1113", max1113 },
276 { },
277};
278MODULE_DEVICE_TABLE(spi, max1111_ids);
279
280static struct spi_driver max1111_driver = {
281 .driver = {
282 .name = "max1111",
283 },
284 .id_table = max1111_ids,
285 .probe = max1111_probe,
286 .remove = max1111_remove,
287};
288
289module_spi_driver(max1111_driver);
290
291MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
292MODULE_DESCRIPTION("MAX1110/MAX1111/MAX1112/MAX1113 ADC Driver");
293MODULE_LICENSE("GPL");