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>
23 #include <linux/slab.h>
25 enum chips
{ max1110
, max1111
, max1112
, max1113
};
27 #define MAX1111_TX_BUF_SIZE 1
28 #define MAX1111_RX_BUF_SIZE 2
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)
35 #define MAX1110_CTRL_SEL_SH (4)
36 #define MAX1111_CTRL_SEL_SH (5) /* NOTE: bit 4 is ignored */
37 #define MAX1111_CTRL_STR (1u << 7)
40 struct spi_device
*spi
;
41 struct device
*hwmon_dev
;
42 struct spi_message msg
;
43 struct spi_transfer xfer
[2];
44 uint8_t tx_buf
[MAX1111_TX_BUF_SIZE
];
45 uint8_t rx_buf
[MAX1111_RX_BUF_SIZE
];
46 struct mutex drvdata_lock
;
47 /* protect msg, xfer and buffers from multiple access */
52 static int max1111_read(struct device
*dev
, int channel
)
54 struct max1111_data
*data
= dev_get_drvdata(dev
);
58 /* writing to drvdata struct is not thread safe, wait on mutex */
59 mutex_lock(&data
->drvdata_lock
);
61 data
->tx_buf
[0] = (channel
<< data
->sel_sh
) |
62 MAX1111_CTRL_PD0
| MAX1111_CTRL_PD1
|
63 MAX1111_CTRL_SGL
| MAX1111_CTRL_UNI
| MAX1111_CTRL_STR
;
65 err
= spi_sync(data
->spi
, &data
->msg
);
67 dev_err(dev
, "spi_sync failed with %d\n", err
);
68 mutex_unlock(&data
->drvdata_lock
);
75 mutex_unlock(&data
->drvdata_lock
);
77 if ((v1
& 0xc0) || (v2
& 0x3f))
80 return (v1
<< 2) | (v2
>> 6);
83 #ifdef CONFIG_SHARPSL_PM
84 static struct max1111_data
*the_max1111
;
86 int max1111_read_channel(int channel
)
88 if (!the_max1111
|| !the_max1111
->spi
)
91 return max1111_read(&the_max1111
->spi
->dev
, channel
);
93 EXPORT_SYMBOL(max1111_read_channel
);
97 * NOTE: SPI devices do not have a default 'name' attribute, which is
98 * likely to be used by hwmon applications to distinguish between
99 * different devices, explicitly add a name attribute here.
101 static ssize_t
show_name(struct device
*dev
,
102 struct device_attribute
*attr
, char *buf
)
104 return sprintf(buf
, "%s\n", to_spi_device(dev
)->modalias
);
107 static ssize_t
show_adc(struct device
*dev
,
108 struct device_attribute
*attr
, char *buf
)
110 struct max1111_data
*data
= dev_get_drvdata(dev
);
111 int channel
= to_sensor_dev_attr(attr
)->index
;
114 ret
= max1111_read(dev
, channel
);
119 * Assume the reference voltage to be 2.048V or 4.096V, with an 8-bit
120 * sample. The LSB weight is 8mV or 16mV depending on the chip type.
122 return sprintf(buf
, "%d\n", ret
* data
->lsb
);
125 #define MAX1111_ADC_ATTR(_id) \
126 SENSOR_DEVICE_ATTR(in##_id##_input, S_IRUGO, show_adc, NULL, _id)
128 static DEVICE_ATTR(name
, S_IRUGO
, show_name
, NULL
);
129 static MAX1111_ADC_ATTR(0);
130 static MAX1111_ADC_ATTR(1);
131 static MAX1111_ADC_ATTR(2);
132 static MAX1111_ADC_ATTR(3);
133 static MAX1111_ADC_ATTR(4);
134 static MAX1111_ADC_ATTR(5);
135 static MAX1111_ADC_ATTR(6);
136 static MAX1111_ADC_ATTR(7);
138 static struct attribute
*max1111_attributes
[] = {
140 &sensor_dev_attr_in0_input
.dev_attr
.attr
,
141 &sensor_dev_attr_in1_input
.dev_attr
.attr
,
142 &sensor_dev_attr_in2_input
.dev_attr
.attr
,
143 &sensor_dev_attr_in3_input
.dev_attr
.attr
,
147 static const struct attribute_group max1111_attr_group
= {
148 .attrs
= max1111_attributes
,
151 static struct attribute
*max1110_attributes
[] = {
152 &sensor_dev_attr_in4_input
.dev_attr
.attr
,
153 &sensor_dev_attr_in5_input
.dev_attr
.attr
,
154 &sensor_dev_attr_in6_input
.dev_attr
.attr
,
155 &sensor_dev_attr_in7_input
.dev_attr
.attr
,
159 static const struct attribute_group max1110_attr_group
= {
160 .attrs
= max1110_attributes
,
163 static int setup_transfer(struct max1111_data
*data
)
165 struct spi_message
*m
;
166 struct spi_transfer
*x
;
173 x
->tx_buf
= &data
->tx_buf
[0];
174 x
->len
= MAX1111_TX_BUF_SIZE
;
175 spi_message_add_tail(x
, m
);
178 x
->rx_buf
= &data
->rx_buf
[0];
179 x
->len
= MAX1111_RX_BUF_SIZE
;
180 spi_message_add_tail(x
, m
);
185 static int max1111_probe(struct spi_device
*spi
)
187 enum chips chip
= spi_get_device_id(spi
)->driver_data
;
188 struct max1111_data
*data
;
191 spi
->bits_per_word
= 8;
192 spi
->mode
= SPI_MODE_0
;
193 err
= spi_setup(spi
);
197 data
= devm_kzalloc(&spi
->dev
, sizeof(struct max1111_data
), GFP_KERNEL
);
199 dev_err(&spi
->dev
, "failed to allocate memory\n");
206 data
->sel_sh
= MAX1110_CTRL_SEL_SH
;
210 data
->sel_sh
= MAX1111_CTRL_SEL_SH
;
214 data
->sel_sh
= MAX1110_CTRL_SEL_SH
;
218 data
->sel_sh
= MAX1111_CTRL_SEL_SH
;
221 err
= setup_transfer(data
);
225 mutex_init(&data
->drvdata_lock
);
228 spi_set_drvdata(spi
, data
);
230 err
= sysfs_create_group(&spi
->dev
.kobj
, &max1111_attr_group
);
232 dev_err(&spi
->dev
, "failed to create attribute group\n");
235 if (chip
== max1110
|| chip
== max1112
) {
236 err
= sysfs_create_group(&spi
->dev
.kobj
, &max1110_attr_group
);
239 "failed to create extended attribute group\n");
244 data
->hwmon_dev
= hwmon_device_register(&spi
->dev
);
245 if (IS_ERR(data
->hwmon_dev
)) {
246 dev_err(&spi
->dev
, "failed to create hwmon device\n");
247 err
= PTR_ERR(data
->hwmon_dev
);
251 #ifdef CONFIG_SHARPSL_PM
257 sysfs_remove_group(&spi
->dev
.kobj
, &max1110_attr_group
);
258 sysfs_remove_group(&spi
->dev
.kobj
, &max1111_attr_group
);
262 static int max1111_remove(struct spi_device
*spi
)
264 struct max1111_data
*data
= spi_get_drvdata(spi
);
266 #ifdef CONFIG_SHARPSL_PM
269 hwmon_device_unregister(data
->hwmon_dev
);
270 sysfs_remove_group(&spi
->dev
.kobj
, &max1110_attr_group
);
271 sysfs_remove_group(&spi
->dev
.kobj
, &max1111_attr_group
);
272 mutex_destroy(&data
->drvdata_lock
);
276 static const struct spi_device_id max1111_ids
[] = {
277 { "max1110", max1110
},
278 { "max1111", max1111
},
279 { "max1112", max1112
},
280 { "max1113", max1113
},
283 MODULE_DEVICE_TABLE(spi
, max1111_ids
);
285 static struct spi_driver max1111_driver
= {
288 .owner
= THIS_MODULE
,
290 .id_table
= max1111_ids
,
291 .probe
= max1111_probe
,
292 .remove
= max1111_remove
,
295 module_spi_driver(max1111_driver
);
297 MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
298 MODULE_DESCRIPTION("MAX1110/MAX1111/MAX1112/MAX1113 ADC Driver");
299 MODULE_LICENSE("GPL");