Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 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 | |
| 36 | struct 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; |
| 42 | uint8_t *rx_buf; |
Pavel Herrmann | d3f684f | 2011-07-17 18:39:19 +0200 | [diff] [blame] | 43 | struct mutex drvdata_lock; |
| 44 | /* protect msg, xfer and buffers from multiple access */ |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | static 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 | |
Pavel Herrmann | d3f684f | 2011-07-17 18:39:19 +0200 | [diff] [blame] | 53 | /* writing to drvdata struct is not thread safe, wait on mutex */ |
| 54 | mutex_lock(&data->drvdata_lock); |
| 55 | |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 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); |
Pavel Herrmann | d3f684f | 2011-07-17 18:39:19 +0200 | [diff] [blame] | 63 | mutex_unlock(&data->drvdata_lock); |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 64 | return err; |
| 65 | } |
| 66 | |
| 67 | v1 = data->rx_buf[0]; |
| 68 | v2 = data->rx_buf[1]; |
| 69 | |
Pavel Herrmann | d3f684f | 2011-07-17 18:39:19 +0200 | [diff] [blame] | 70 | mutex_unlock(&data->drvdata_lock); |
| 71 | |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 72 | if ((v1 & 0xc0) || (v2 & 0x3f)) |
| 73 | return -EINVAL; |
| 74 | |
| 75 | return (v1 << 2) | (v2 >> 6); |
| 76 | } |
| 77 | |
Eric Miao | f16177c | 2008-08-29 06:19:32 +0800 | [diff] [blame] | 78 | #ifdef CONFIG_SHARPSL_PM |
| 79 | static struct max1111_data *the_max1111; |
| 80 | |
| 81 | int max1111_read_channel(int channel) |
| 82 | { |
| 83 | return max1111_read(&the_max1111->spi->dev, channel); |
| 84 | } |
| 85 | EXPORT_SYMBOL(max1111_read_channel); |
| 86 | #endif |
| 87 | |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 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 | */ |
| 93 | static ssize_t show_name(struct device *dev, |
| 94 | struct device_attribute *attr, char *buf) |
| 95 | { |
| 96 | return sprintf(buf, "max1111\n"); |
| 97 | } |
| 98 | |
| 99 | static 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 | |
| 115 | static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); |
| 116 | static MAX1111_ADC_ATTR(0); |
| 117 | static MAX1111_ADC_ATTR(1); |
| 118 | static MAX1111_ADC_ATTR(2); |
| 119 | static MAX1111_ADC_ATTR(3); |
| 120 | |
| 121 | static 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 | |
| 130 | static const struct attribute_group max1111_attr_group = { |
| 131 | .attrs = max1111_attributes, |
| 132 | }; |
| 133 | |
| 134 | static int setup_transfer(struct max1111_data *data) |
| 135 | { |
| 136 | struct spi_message *m; |
| 137 | struct spi_transfer *x; |
| 138 | |
| 139 | data->tx_buf = kmalloc(MAX1111_TX_BUF_SIZE, GFP_KERNEL); |
| 140 | if (!data->tx_buf) |
| 141 | return -ENOMEM; |
| 142 | |
| 143 | data->rx_buf = kmalloc(MAX1111_RX_BUF_SIZE, GFP_KERNEL); |
| 144 | if (!data->rx_buf) { |
| 145 | kfree(data->tx_buf); |
| 146 | return -ENOMEM; |
| 147 | } |
| 148 | |
| 149 | m = &data->msg; |
| 150 | x = &data->xfer[0]; |
| 151 | |
| 152 | spi_message_init(m); |
| 153 | |
| 154 | x->tx_buf = &data->tx_buf[0]; |
| 155 | x->len = 1; |
| 156 | spi_message_add_tail(x, m); |
| 157 | |
| 158 | x++; |
| 159 | x->rx_buf = &data->rx_buf[0]; |
| 160 | x->len = 2; |
| 161 | spi_message_add_tail(x, m); |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static int __devinit max1111_probe(struct spi_device *spi) |
| 167 | { |
| 168 | struct max1111_data *data; |
| 169 | int err; |
| 170 | |
| 171 | spi->bits_per_word = 8; |
| 172 | spi->mode = SPI_MODE_0; |
| 173 | err = spi_setup(spi); |
| 174 | if (err < 0) |
| 175 | return err; |
| 176 | |
| 177 | data = kzalloc(sizeof(struct max1111_data), GFP_KERNEL); |
| 178 | if (data == NULL) { |
| 179 | dev_err(&spi->dev, "failed to allocate memory\n"); |
| 180 | return -ENOMEM; |
| 181 | } |
| 182 | |
| 183 | err = setup_transfer(data); |
| 184 | if (err) |
| 185 | goto err_free_data; |
| 186 | |
Pavel Herrmann | d3f684f | 2011-07-17 18:39:19 +0200 | [diff] [blame] | 187 | mutex_init(&data->drvdata_lock); |
| 188 | |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 189 | data->spi = spi; |
| 190 | spi_set_drvdata(spi, data); |
| 191 | |
| 192 | err = sysfs_create_group(&spi->dev.kobj, &max1111_attr_group); |
| 193 | if (err) { |
| 194 | dev_err(&spi->dev, "failed to create attribute group\n"); |
| 195 | goto err_free_all; |
| 196 | } |
| 197 | |
| 198 | data->hwmon_dev = hwmon_device_register(&spi->dev); |
| 199 | if (IS_ERR(data->hwmon_dev)) { |
| 200 | dev_err(&spi->dev, "failed to create hwmon device\n"); |
| 201 | err = PTR_ERR(data->hwmon_dev); |
| 202 | goto err_remove; |
| 203 | } |
| 204 | |
Eric Miao | f16177c | 2008-08-29 06:19:32 +0800 | [diff] [blame] | 205 | #ifdef CONFIG_SHARPSL_PM |
| 206 | the_max1111 = data; |
| 207 | #endif |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 208 | return 0; |
| 209 | |
| 210 | err_remove: |
| 211 | sysfs_remove_group(&spi->dev.kobj, &max1111_attr_group); |
| 212 | err_free_all: |
| 213 | kfree(data->rx_buf); |
| 214 | kfree(data->tx_buf); |
| 215 | err_free_data: |
| 216 | kfree(data); |
| 217 | return err; |
| 218 | } |
| 219 | |
| 220 | static int __devexit max1111_remove(struct spi_device *spi) |
| 221 | { |
| 222 | struct max1111_data *data = spi_get_drvdata(spi); |
| 223 | |
| 224 | hwmon_device_unregister(data->hwmon_dev); |
| 225 | sysfs_remove_group(&spi->dev.kobj, &max1111_attr_group); |
Pavel Herrmann | d3f684f | 2011-07-17 18:39:19 +0200 | [diff] [blame] | 226 | mutex_destroy(&data->drvdata_lock); |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 227 | kfree(data->rx_buf); |
| 228 | kfree(data->tx_buf); |
| 229 | kfree(data); |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | static struct spi_driver max1111_driver = { |
| 234 | .driver = { |
| 235 | .name = "max1111", |
| 236 | .owner = THIS_MODULE, |
| 237 | }, |
| 238 | .probe = max1111_probe, |
| 239 | .remove = __devexit_p(max1111_remove), |
| 240 | }; |
| 241 | |
| 242 | static int __init max1111_init(void) |
| 243 | { |
| 244 | return spi_register_driver(&max1111_driver); |
| 245 | } |
| 246 | module_init(max1111_init); |
| 247 | |
| 248 | static void __exit max1111_exit(void) |
| 249 | { |
| 250 | spi_unregister_driver(&max1111_driver); |
| 251 | } |
| 252 | module_exit(max1111_exit); |
| 253 | |
| 254 | MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>"); |
| 255 | MODULE_DESCRIPTION("MAX1111 ADC Driver"); |
| 256 | MODULE_LICENSE("GPL"); |
Anton Vorontsov | e0626e3 | 2009-09-22 16:46:08 -0700 | [diff] [blame] | 257 | MODULE_ALIAS("spi:max1111"); |