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. |
Frans Meulenbroeks | 91c8eab | 2012-01-04 23:27:19 +0100 | [diff] [blame] | 9 | * Eric Miao <eric.miao@marvell.com> |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 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 | |
Guenter Roeck | 9224c38 | 2012-08-20 18:52:36 -0700 | [diff] [blame] | 25 | enum chips { max1110, max1111, max1112, max1113 }; |
| 26 | |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 27 | #define MAX1111_TX_BUF_SIZE 1 |
| 28 | #define MAX1111_RX_BUF_SIZE 2 |
| 29 | |
| 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) |
Guenter Roeck | 9224c38 | 2012-08-20 18:52:36 -0700 | [diff] [blame] | 35 | #define MAX1110_CTRL_SEL_SH (4) |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 36 | #define MAX1111_CTRL_SEL_SH (5) /* NOTE: bit 4 is ignored */ |
| 37 | #define MAX1111_CTRL_STR (1u << 7) |
| 38 | |
| 39 | struct max1111_data { |
| 40 | struct spi_device *spi; |
| 41 | struct device *hwmon_dev; |
| 42 | struct spi_message msg; |
| 43 | struct spi_transfer xfer[2]; |
Jean Delvare | d737c09 | 2011-07-25 21:46:10 +0200 | [diff] [blame] | 44 | uint8_t tx_buf[MAX1111_TX_BUF_SIZE]; |
| 45 | uint8_t rx_buf[MAX1111_RX_BUF_SIZE]; |
Pavel Herrmann | d3f684f | 2011-07-17 18:39:19 +0200 | [diff] [blame] | 46 | struct mutex drvdata_lock; |
| 47 | /* protect msg, xfer and buffers from multiple access */ |
Guenter Roeck | 9224c38 | 2012-08-20 18:52:36 -0700 | [diff] [blame] | 48 | int sel_sh; |
| 49 | int lsb; |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | static int max1111_read(struct device *dev, int channel) |
| 53 | { |
| 54 | struct max1111_data *data = dev_get_drvdata(dev); |
| 55 | uint8_t v1, v2; |
| 56 | int err; |
| 57 | |
Pavel Herrmann | d3f684f | 2011-07-17 18:39:19 +0200 | [diff] [blame] | 58 | /* writing to drvdata struct is not thread safe, wait on mutex */ |
| 59 | mutex_lock(&data->drvdata_lock); |
| 60 | |
Guenter Roeck | 9224c38 | 2012-08-20 18:52:36 -0700 | [diff] [blame] | 61 | data->tx_buf[0] = (channel << data->sel_sh) | |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 62 | MAX1111_CTRL_PD0 | MAX1111_CTRL_PD1 | |
| 63 | MAX1111_CTRL_SGL | MAX1111_CTRL_UNI | MAX1111_CTRL_STR; |
| 64 | |
| 65 | err = spi_sync(data->spi, &data->msg); |
| 66 | if (err < 0) { |
| 67 | dev_err(dev, "spi_sync failed with %d\n", err); |
Pavel Herrmann | d3f684f | 2011-07-17 18:39:19 +0200 | [diff] [blame] | 68 | mutex_unlock(&data->drvdata_lock); |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 69 | return err; |
| 70 | } |
| 71 | |
| 72 | v1 = data->rx_buf[0]; |
| 73 | v2 = data->rx_buf[1]; |
| 74 | |
Pavel Herrmann | d3f684f | 2011-07-17 18:39:19 +0200 | [diff] [blame] | 75 | mutex_unlock(&data->drvdata_lock); |
| 76 | |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 77 | if ((v1 & 0xc0) || (v2 & 0x3f)) |
| 78 | return -EINVAL; |
| 79 | |
| 80 | return (v1 << 2) | (v2 >> 6); |
| 81 | } |
| 82 | |
Eric Miao | f16177c | 2008-08-29 06:19:32 +0800 | [diff] [blame] | 83 | #ifdef CONFIG_SHARPSL_PM |
| 84 | static struct max1111_data *the_max1111; |
| 85 | |
| 86 | int max1111_read_channel(int channel) |
| 87 | { |
Guenter Roeck | 3c2e226 | 2016-03-26 12:28:05 -0700 | [diff] [blame] | 88 | if (!the_max1111 || !the_max1111->spi) |
| 89 | return -ENODEV; |
| 90 | |
Eric Miao | f16177c | 2008-08-29 06:19:32 +0800 | [diff] [blame] | 91 | return max1111_read(&the_max1111->spi->dev, channel); |
| 92 | } |
| 93 | EXPORT_SYMBOL(max1111_read_channel); |
| 94 | #endif |
| 95 | |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 96 | /* |
| 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. |
| 100 | */ |
| 101 | static ssize_t show_name(struct device *dev, |
| 102 | struct device_attribute *attr, char *buf) |
| 103 | { |
Guenter Roeck | 9224c38 | 2012-08-20 18:52:36 -0700 | [diff] [blame] | 104 | return sprintf(buf, "%s\n", to_spi_device(dev)->modalias); |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static ssize_t show_adc(struct device *dev, |
| 108 | struct device_attribute *attr, char *buf) |
| 109 | { |
Guenter Roeck | 9224c38 | 2012-08-20 18:52:36 -0700 | [diff] [blame] | 110 | struct max1111_data *data = dev_get_drvdata(dev); |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 111 | int channel = to_sensor_dev_attr(attr)->index; |
| 112 | int ret; |
| 113 | |
| 114 | ret = max1111_read(dev, channel); |
| 115 | if (ret < 0) |
| 116 | return ret; |
| 117 | |
Guenter Roeck | 51683ee | 2012-01-19 11:02:22 -0800 | [diff] [blame] | 118 | /* |
Guenter Roeck | 9224c38 | 2012-08-20 18:52:36 -0700 | [diff] [blame] | 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. |
Eric Miao | 012f3b9 | 2012-01-16 22:51:45 +0100 | [diff] [blame] | 121 | */ |
Guenter Roeck | 9224c38 | 2012-08-20 18:52:36 -0700 | [diff] [blame] | 122 | return sprintf(buf, "%d\n", ret * data->lsb); |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | #define MAX1111_ADC_ATTR(_id) \ |
Eric Miao | 012f3b9 | 2012-01-16 22:51:45 +0100 | [diff] [blame] | 126 | SENSOR_DEVICE_ATTR(in##_id##_input, S_IRUGO, show_adc, NULL, _id) |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 127 | |
| 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); |
Guenter Roeck | 9224c38 | 2012-08-20 18:52:36 -0700 | [diff] [blame] | 133 | static MAX1111_ADC_ATTR(4); |
| 134 | static MAX1111_ADC_ATTR(5); |
| 135 | static MAX1111_ADC_ATTR(6); |
| 136 | static MAX1111_ADC_ATTR(7); |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 137 | |
| 138 | static struct attribute *max1111_attributes[] = { |
| 139 | &dev_attr_name.attr, |
Eric Miao | 012f3b9 | 2012-01-16 22:51:45 +0100 | [diff] [blame] | 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, |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 144 | NULL, |
| 145 | }; |
| 146 | |
| 147 | static const struct attribute_group max1111_attr_group = { |
| 148 | .attrs = max1111_attributes, |
| 149 | }; |
| 150 | |
Guenter Roeck | 9224c38 | 2012-08-20 18:52:36 -0700 | [diff] [blame] | 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, |
| 156 | NULL, |
| 157 | }; |
| 158 | |
| 159 | static const struct attribute_group max1110_attr_group = { |
| 160 | .attrs = max1110_attributes, |
| 161 | }; |
| 162 | |
Bill Pemberton | 6c931ae | 2012-11-19 13:22:35 -0500 | [diff] [blame] | 163 | static int setup_transfer(struct max1111_data *data) |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 164 | { |
| 165 | struct spi_message *m; |
| 166 | struct spi_transfer *x; |
| 167 | |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 168 | m = &data->msg; |
| 169 | x = &data->xfer[0]; |
| 170 | |
| 171 | spi_message_init(m); |
| 172 | |
| 173 | x->tx_buf = &data->tx_buf[0]; |
Jean Delvare | d737c09 | 2011-07-25 21:46:10 +0200 | [diff] [blame] | 174 | x->len = MAX1111_TX_BUF_SIZE; |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 175 | spi_message_add_tail(x, m); |
| 176 | |
| 177 | x++; |
| 178 | x->rx_buf = &data->rx_buf[0]; |
Jean Delvare | d737c09 | 2011-07-25 21:46:10 +0200 | [diff] [blame] | 179 | x->len = MAX1111_RX_BUF_SIZE; |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 180 | spi_message_add_tail(x, m); |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
Bill Pemberton | 6c931ae | 2012-11-19 13:22:35 -0500 | [diff] [blame] | 185 | static int max1111_probe(struct spi_device *spi) |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 186 | { |
Guenter Roeck | 9224c38 | 2012-08-20 18:52:36 -0700 | [diff] [blame] | 187 | enum chips chip = spi_get_device_id(spi)->driver_data; |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 188 | struct max1111_data *data; |
| 189 | int err; |
| 190 | |
| 191 | spi->bits_per_word = 8; |
| 192 | spi->mode = SPI_MODE_0; |
| 193 | err = spi_setup(spi); |
| 194 | if (err < 0) |
| 195 | return err; |
| 196 | |
Guenter Roeck | 661f020 | 2012-06-02 09:58:12 -0700 | [diff] [blame] | 197 | data = devm_kzalloc(&spi->dev, sizeof(struct max1111_data), GFP_KERNEL); |
Jingoo Han | 611cd8a | 2014-04-29 17:10:17 +0900 | [diff] [blame] | 198 | if (data == NULL) |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 199 | return -ENOMEM; |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 200 | |
Guenter Roeck | 9224c38 | 2012-08-20 18:52:36 -0700 | [diff] [blame] | 201 | switch (chip) { |
| 202 | case max1110: |
| 203 | data->lsb = 8; |
| 204 | data->sel_sh = MAX1110_CTRL_SEL_SH; |
| 205 | break; |
| 206 | case max1111: |
| 207 | data->lsb = 8; |
| 208 | data->sel_sh = MAX1111_CTRL_SEL_SH; |
| 209 | break; |
| 210 | case max1112: |
| 211 | data->lsb = 16; |
| 212 | data->sel_sh = MAX1110_CTRL_SEL_SH; |
| 213 | break; |
| 214 | case max1113: |
| 215 | data->lsb = 16; |
| 216 | data->sel_sh = MAX1111_CTRL_SEL_SH; |
| 217 | break; |
| 218 | } |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 219 | err = setup_transfer(data); |
| 220 | if (err) |
Guenter Roeck | 661f020 | 2012-06-02 09:58:12 -0700 | [diff] [blame] | 221 | return err; |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 222 | |
Pavel Herrmann | d3f684f | 2011-07-17 18:39:19 +0200 | [diff] [blame] | 223 | mutex_init(&data->drvdata_lock); |
| 224 | |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 225 | data->spi = spi; |
| 226 | spi_set_drvdata(spi, data); |
| 227 | |
| 228 | err = sysfs_create_group(&spi->dev.kobj, &max1111_attr_group); |
| 229 | if (err) { |
| 230 | dev_err(&spi->dev, "failed to create attribute group\n"); |
Guenter Roeck | 661f020 | 2012-06-02 09:58:12 -0700 | [diff] [blame] | 231 | return err; |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 232 | } |
Guenter Roeck | 9224c38 | 2012-08-20 18:52:36 -0700 | [diff] [blame] | 233 | if (chip == max1110 || chip == max1112) { |
| 234 | err = sysfs_create_group(&spi->dev.kobj, &max1110_attr_group); |
| 235 | if (err) { |
| 236 | dev_err(&spi->dev, |
| 237 | "failed to create extended attribute group\n"); |
| 238 | goto err_remove; |
| 239 | } |
| 240 | } |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 241 | |
| 242 | data->hwmon_dev = hwmon_device_register(&spi->dev); |
| 243 | if (IS_ERR(data->hwmon_dev)) { |
| 244 | dev_err(&spi->dev, "failed to create hwmon device\n"); |
| 245 | err = PTR_ERR(data->hwmon_dev); |
| 246 | goto err_remove; |
| 247 | } |
| 248 | |
Eric Miao | f16177c | 2008-08-29 06:19:32 +0800 | [diff] [blame] | 249 | #ifdef CONFIG_SHARPSL_PM |
| 250 | the_max1111 = data; |
| 251 | #endif |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 252 | return 0; |
| 253 | |
| 254 | err_remove: |
Guenter Roeck | 9224c38 | 2012-08-20 18:52:36 -0700 | [diff] [blame] | 255 | sysfs_remove_group(&spi->dev.kobj, &max1110_attr_group); |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 256 | sysfs_remove_group(&spi->dev.kobj, &max1111_attr_group); |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 257 | return err; |
| 258 | } |
| 259 | |
Bill Pemberton | 281dfd0 | 2012-11-19 13:25:51 -0500 | [diff] [blame] | 260 | static int max1111_remove(struct spi_device *spi) |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 261 | { |
| 262 | struct max1111_data *data = spi_get_drvdata(spi); |
| 263 | |
Guenter Roeck | 3c2e226 | 2016-03-26 12:28:05 -0700 | [diff] [blame] | 264 | #ifdef CONFIG_SHARPSL_PM |
| 265 | the_max1111 = NULL; |
| 266 | #endif |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 267 | hwmon_device_unregister(data->hwmon_dev); |
Guenter Roeck | 9224c38 | 2012-08-20 18:52:36 -0700 | [diff] [blame] | 268 | sysfs_remove_group(&spi->dev.kobj, &max1110_attr_group); |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 269 | sysfs_remove_group(&spi->dev.kobj, &max1111_attr_group); |
Pavel Herrmann | d3f684f | 2011-07-17 18:39:19 +0200 | [diff] [blame] | 270 | mutex_destroy(&data->drvdata_lock); |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 271 | return 0; |
| 272 | } |
| 273 | |
Guenter Roeck | 9224c38 | 2012-08-20 18:52:36 -0700 | [diff] [blame] | 274 | static const struct spi_device_id max1111_ids[] = { |
| 275 | { "max1110", max1110 }, |
| 276 | { "max1111", max1111 }, |
| 277 | { "max1112", max1112 }, |
| 278 | { "max1113", max1113 }, |
| 279 | { }, |
| 280 | }; |
| 281 | MODULE_DEVICE_TABLE(spi, max1111_ids); |
| 282 | |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 283 | static struct spi_driver max1111_driver = { |
| 284 | .driver = { |
| 285 | .name = "max1111", |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 286 | }, |
Guenter Roeck | 9224c38 | 2012-08-20 18:52:36 -0700 | [diff] [blame] | 287 | .id_table = max1111_ids, |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 288 | .probe = max1111_probe, |
Bill Pemberton | 9e5e9b7 | 2012-11-19 13:21:20 -0500 | [diff] [blame] | 289 | .remove = max1111_remove, |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 290 | }; |
| 291 | |
Axel Lin | 91efffe | 2012-01-20 15:53:47 +0800 | [diff] [blame] | 292 | module_spi_driver(max1111_driver); |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 293 | |
| 294 | MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>"); |
Guenter Roeck | 9224c38 | 2012-08-20 18:52:36 -0700 | [diff] [blame] | 295 | MODULE_DESCRIPTION("MAX1110/MAX1111/MAX1112/MAX1113 ADC Driver"); |
Eric Miao | 55b951e | 2008-08-29 04:26:48 +0800 | [diff] [blame] | 296 | MODULE_LICENSE("GPL"); |