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 #define MAX1111_TX_BUF_SIZE 1
26 #define MAX1111_RX_BUF_SIZE 2
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)
37 struct spi_device
*spi
;
38 struct device
*hwmon_dev
;
39 struct spi_message msg
;
40 struct spi_transfer xfer
[2];
45 static int max1111_read(struct device
*dev
, int channel
)
47 struct max1111_data
*data
= dev_get_drvdata(dev
);
51 data
->tx_buf
[0] = (channel
<< MAX1111_CTRL_SEL_SH
) |
52 MAX1111_CTRL_PD0
| MAX1111_CTRL_PD1
|
53 MAX1111_CTRL_SGL
| MAX1111_CTRL_UNI
| MAX1111_CTRL_STR
;
55 err
= spi_sync(data
->spi
, &data
->msg
);
57 dev_err(dev
, "spi_sync failed with %d\n", err
);
64 if ((v1
& 0xc0) || (v2
& 0x3f))
67 return (v1
<< 2) | (v2
>> 6);
70 #ifdef CONFIG_SHARPSL_PM
71 static struct max1111_data
*the_max1111
;
73 int max1111_read_channel(int channel
)
75 return max1111_read(&the_max1111
->spi
->dev
, channel
);
77 EXPORT_SYMBOL(max1111_read_channel
);
81 * NOTE: SPI devices do not have a default 'name' attribute, which is
82 * likely to be used by hwmon applications to distinguish between
83 * different devices, explicitly add a name attribute here.
85 static ssize_t
show_name(struct device
*dev
,
86 struct device_attribute
*attr
, char *buf
)
88 return sprintf(buf
, "max1111\n");
91 static ssize_t
show_adc(struct device
*dev
,
92 struct device_attribute
*attr
, char *buf
)
94 int channel
= to_sensor_dev_attr(attr
)->index
;
97 ret
= max1111_read(dev
, channel
);
101 return sprintf(buf
, "%d\n", ret
);
104 #define MAX1111_ADC_ATTR(_id) \
105 SENSOR_DEVICE_ATTR(adc##_id##_in, S_IRUGO, show_adc, NULL, _id)
107 static DEVICE_ATTR(name
, S_IRUGO
, show_name
, NULL
);
108 static MAX1111_ADC_ATTR(0);
109 static MAX1111_ADC_ATTR(1);
110 static MAX1111_ADC_ATTR(2);
111 static MAX1111_ADC_ATTR(3);
113 static struct attribute
*max1111_attributes
[] = {
115 &sensor_dev_attr_adc0_in
.dev_attr
.attr
,
116 &sensor_dev_attr_adc1_in
.dev_attr
.attr
,
117 &sensor_dev_attr_adc2_in
.dev_attr
.attr
,
118 &sensor_dev_attr_adc3_in
.dev_attr
.attr
,
122 static const struct attribute_group max1111_attr_group
= {
123 .attrs
= max1111_attributes
,
126 static int setup_transfer(struct max1111_data
*data
)
128 struct spi_message
*m
;
129 struct spi_transfer
*x
;
131 data
->tx_buf
= kmalloc(MAX1111_TX_BUF_SIZE
, GFP_KERNEL
);
135 data
->rx_buf
= kmalloc(MAX1111_RX_BUF_SIZE
, GFP_KERNEL
);
146 x
->tx_buf
= &data
->tx_buf
[0];
148 spi_message_add_tail(x
, m
);
151 x
->rx_buf
= &data
->rx_buf
[0];
153 spi_message_add_tail(x
, m
);
158 static int __devinit
max1111_probe(struct spi_device
*spi
)
160 struct max1111_data
*data
;
163 spi
->bits_per_word
= 8;
164 spi
->mode
= SPI_MODE_0
;
165 err
= spi_setup(spi
);
169 data
= kzalloc(sizeof(struct max1111_data
), GFP_KERNEL
);
171 dev_err(&spi
->dev
, "failed to allocate memory\n");
175 err
= setup_transfer(data
);
180 spi_set_drvdata(spi
, data
);
182 err
= sysfs_create_group(&spi
->dev
.kobj
, &max1111_attr_group
);
184 dev_err(&spi
->dev
, "failed to create attribute group\n");
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
);
195 #ifdef CONFIG_SHARPSL_PM
201 sysfs_remove_group(&spi
->dev
.kobj
, &max1111_attr_group
);
210 static int __devexit
max1111_remove(struct spi_device
*spi
)
212 struct max1111_data
*data
= spi_get_drvdata(spi
);
214 hwmon_device_unregister(data
->hwmon_dev
);
215 sysfs_remove_group(&spi
->dev
.kobj
, &max1111_attr_group
);
222 static struct spi_driver max1111_driver
= {
225 .owner
= THIS_MODULE
,
227 .probe
= max1111_probe
,
228 .remove
= __devexit_p(max1111_remove
),
231 static int __init
max1111_init(void)
233 return spi_register_driver(&max1111_driver
);
235 module_init(max1111_init
);
237 static void __exit
max1111_exit(void)
239 spi_unregister_driver(&max1111_driver
);
241 module_exit(max1111_exit
);
243 MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
244 MODULE_DESCRIPTION("MAX1111 ADC Driver");
245 MODULE_LICENSE("GPL");
246 MODULE_ALIAS("spi:max1111");