1 // SPDX-License-Identifier: GPL-2.0-only
3 * max1111.c - +2.7V, Low-Power, Multichannel, Serial 8-bit ADCs
5 * Based on arch/arm/mach-pxa/corgi_ssp.c
7 * Copyright (C) 2004-2005 Richard Purdie
9 * Copyright (C) 2008 Marvell International Ltd.
10 * Eric Miao <eric.miao@marvell.com>
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>
22 enum chips { max1110, max1111, max1112, max1113 };
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 MAX1110_CTRL_SEL_SH (4)
33 #define MAX1111_CTRL_SEL_SH (5) /* NOTE: bit 4 is ignored */
34 #define MAX1111_CTRL_STR (1u << 7)
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 */
49 static int max1111_read(struct device *dev, int channel)
51 struct max1111_data *data = dev_get_drvdata(dev);
55 /* writing to drvdata struct is not thread safe, wait on mutex */
56 mutex_lock(&data->drvdata_lock);
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;
62 err = spi_sync(data->spi, &data->msg);
64 dev_err(dev, "spi_sync failed with %d\n", err);
65 mutex_unlock(&data->drvdata_lock);
72 mutex_unlock(&data->drvdata_lock);
74 if ((v1 & 0xc0) || (v2 & 0x3f))
77 return (v1 << 2) | (v2 >> 6);
80 #ifdef CONFIG_SHARPSL_PM
81 static struct max1111_data *the_max1111;
83 int max1111_read_channel(int channel);
84 int max1111_read_channel(int channel)
86 if (!the_max1111 || !the_max1111->spi)
89 return max1111_read(&the_max1111->spi->dev, channel);
91 EXPORT_SYMBOL(max1111_read_channel);
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.
99 static ssize_t name_show(struct device *dev,
100 struct device_attribute *attr, char *buf)
102 return sprintf(buf, "%s\n", to_spi_device(dev)->modalias);
105 static ssize_t show_adc(struct device *dev,
106 struct device_attribute *attr, char *buf)
108 struct max1111_data *data = dev_get_drvdata(dev);
109 int channel = to_sensor_dev_attr(attr)->index;
112 ret = max1111_read(dev, channel);
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.
120 return sprintf(buf, "%d\n", ret * data->lsb);
123 #define MAX1111_ADC_ATTR(_id) \
124 SENSOR_DEVICE_ATTR(in##_id##_input, S_IRUGO, show_adc, NULL, _id)
126 static DEVICE_ATTR_RO(name);
127 static MAX1111_ADC_ATTR(0);
128 static MAX1111_ADC_ATTR(1);
129 static MAX1111_ADC_ATTR(2);
130 static MAX1111_ADC_ATTR(3);
131 static MAX1111_ADC_ATTR(4);
132 static MAX1111_ADC_ATTR(5);
133 static MAX1111_ADC_ATTR(6);
134 static MAX1111_ADC_ATTR(7);
136 static struct attribute *max1111_attributes[] = {
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,
145 static const struct attribute_group max1111_attr_group = {
146 .attrs = max1111_attributes,
149 static 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,
157 static const struct attribute_group max1110_attr_group = {
158 .attrs = max1110_attributes,
161 static int setup_transfer(struct max1111_data *data)
163 struct spi_message *m;
164 struct spi_transfer *x;
171 x->tx_buf = &data->tx_buf[0];
172 x->len = MAX1111_TX_BUF_SIZE;
173 spi_message_add_tail(x, m);
176 x->rx_buf = &data->rx_buf[0];
177 x->len = MAX1111_RX_BUF_SIZE;
178 spi_message_add_tail(x, m);
183 static int max1111_probe(struct spi_device *spi)
185 enum chips chip = spi_get_device_id(spi)->driver_data;
186 struct max1111_data *data;
189 spi->bits_per_word = 8;
190 spi->mode = SPI_MODE_0;
191 err = spi_setup(spi);
195 data = devm_kzalloc(&spi->dev, sizeof(struct max1111_data), GFP_KERNEL);
202 data->sel_sh = MAX1110_CTRL_SEL_SH;
206 data->sel_sh = MAX1111_CTRL_SEL_SH;
210 data->sel_sh = MAX1110_CTRL_SEL_SH;
214 data->sel_sh = MAX1111_CTRL_SEL_SH;
217 err = setup_transfer(data);
221 mutex_init(&data->drvdata_lock);
224 spi_set_drvdata(spi, data);
226 err = sysfs_create_group(&spi->dev.kobj, &max1111_attr_group);
228 dev_err(&spi->dev, "failed to create attribute group\n");
231 if (chip == max1110 || chip == max1112) {
232 err = sysfs_create_group(&spi->dev.kobj, &max1110_attr_group);
235 "failed to create extended attribute group\n");
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);
247 #ifdef CONFIG_SHARPSL_PM
253 sysfs_remove_group(&spi->dev.kobj, &max1110_attr_group);
254 sysfs_remove_group(&spi->dev.kobj, &max1111_attr_group);
258 static void max1111_remove(struct spi_device *spi)
260 struct max1111_data *data = spi_get_drvdata(spi);
262 #ifdef CONFIG_SHARPSL_PM
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);
271 static const struct spi_device_id max1111_ids[] = {
272 { "max1110", max1110 },
273 { "max1111", max1111 },
274 { "max1112", max1112 },
275 { "max1113", max1113 },
278 MODULE_DEVICE_TABLE(spi, max1111_ids);
280 static struct spi_driver max1111_driver = {
284 .id_table = max1111_ids,
285 .probe = max1111_probe,
286 .remove = max1111_remove,
289 module_spi_driver(max1111_driver);
291 MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
292 MODULE_DESCRIPTION("MAX1110/MAX1111/MAX1112/MAX1113 ADC Driver");
293 MODULE_LICENSE("GPL");