blob: 0bd9cbcccb06fef2378db629133c2b628fd23aa6 [file] [log] [blame]
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -07001/* Copyright (c) 2012, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -070013#include <linux/kernel.h>
14#include <linux/init.h>
Siddartha Mohanadosseb90c922012-03-27 22:58:06 -070015#include <linux/fs.h>
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -070016#include <linux/mutex.h>
17#include <linux/err.h>
18#include <linux/slab.h>
19#include <linux/gpio.h>
20#include <linux/hwmon.h>
21#include <linux/delay.h>
22#include <linux/epm_adc.h>
Siddartha Mohanadosseb90c922012-03-27 22:58:06 -070023#include <linux/uaccess.h>
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -070024#include <linux/spi/spi.h>
25#include <linux/hwmon-sysfs.h>
Siddartha Mohanadosseb90c922012-03-27 22:58:06 -070026#include <linux/miscdevice.h>
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -070027#include <linux/platform_device.h>
28
29#define EPM_ADC_DRIVER_NAME "epm_adc"
30#define EPM_ADC_MAX_FNAME 20
31#define EPM_ADC_CONVERSION_DELAY 100 /* milliseconds */
32/* Command Bits */
33#define EPM_ADC_ADS_SPI_BITS_PER_WORD 8
34#define EPM_ADC_ADS_DATA_READ_CMD (0x1 << 5)
35#define EPM_ADC_ADS_REG_READ_CMD (0x2 << 5)
36#define EPM_ADC_ADS_REG_WRITE_CMD (0x3 << 5)
37#define EPM_ADC_ADS_PULSE_CONVERT_CMD (0x4 << 5)
38#define EPM_ADC_ADS_MULTIPLE_REG_ACCESS (0x1 << 4)
39/* Register map */
40#define EPM_ADC_ADS_CONFIG0_REG_ADDR 0x0
41#define EPM_ADC_ADS_CONFIG1_REG_ADDR 0x1
42#define EPM_ADC_ADS_MUXSG0_REG_ADDR 0x4
43#define EPM_ADC_ADS_MUXSG1_REG_ADDR 0x5
44/* Register map default data */
45#define EPM_ADC_ADS_REG0_DEFAULT 0x2
46#define EPM_ADC_ADS_REG1_DEFAULT 0x52
47#define EPM_ADC_ADS_CHANNEL_DATA_CHID 0x1f
48/* Channel ID */
49#define EPM_ADC_ADS_CHANNEL_OFFSET 0x18
50#define EPM_ADC_ADS_CHANNEL_VCC 0x1a
51#define EPM_ADC_ADS_CHANNEL_TEMP 0x1b
52#define EPM_ADC_ADS_CHANNEL_GAIN 0x1c
53#define EPM_ADC_ADS_CHANNEL_REF 0x1d
54/* Scaling data co-efficients */
55#define EPM_ADC_SCALE_MILLI 1000
56#define EPM_ADC_SCALE_CODE_VOLTS 3072
57#define EPM_ADC_SCALE_CODE_GAIN 30720
58#define EPM_ADC_TEMP_SENSOR_COEFF 394
59#define EPM_ADC_TEMP_TO_DEGC_COEFF 168000
60#define EPM_ADC_CHANNEL_AIN_OFFSET 8
61#define EPM_ADC_MAX_NEGATIVE_SCALE_CODE 0x8000
62#define EPM_ADC_NEG_LSB_CODE 0xffff
63#define EPM_ADC_VREF_CODE 0x7800
64#define EPM_ADC_MILLI_VOLTS_SOURCE 4750
65#define EPM_ADC_SCALE_FACTOR 64
66#define GPIO_EPM_GLOBAL_ENABLE 86
67#define EPM_ADC_CONVERSION_TIME_MIN 50000
68#define EPM_ADC_CONVERSION_TIME_MAX 51000
69
70struct epm_adc_drv {
71 struct platform_device *pdev;
72 struct device *hwmon;
73 struct sensor_device_attribute *sens_attr;
74 char **fnames;
75 struct spi_device *epm_spi_client;
76 struct mutex conv_lock;
77 uint32_t bus_id;
Siddartha Mohanadosseb90c922012-03-27 22:58:06 -070078 struct miscdevice misc;
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -070079};
80
81static struct epm_adc_drv *epm_adc_drv;
82static struct i2c_board_info *epm_i2c_info;
83static bool epm_adc_first_request;
84static int epm_gpio_expander_base_addr;
Siddartha Mohanadosseb90c922012-03-27 22:58:06 -070085static bool epm_adc_expander_register;
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -070086
87#define GPIO_EPM_EXPANDER_IO0 epm_gpio_expander_base_addr
88#define GPIO_PWR_MON_ENABLE (GPIO_EPM_EXPANDER_IO0 + 1)
89#define GPIO_ADC1_PWDN_N (GPIO_PWR_MON_ENABLE + 1)
90#define GPIO_PWR_MON_RESET_N (GPIO_ADC1_PWDN_N + 1)
91#define GPIO_EPM_SPI_ADC1_CS_N (GPIO_PWR_MON_RESET_N + 1)
92#define GPIO_PWR_MON_START (GPIO_EPM_SPI_ADC1_CS_N + 1)
93#define GPIO_ADC1_DRDY_N (GPIO_PWR_MON_START + 1)
94#define GPIO_ADC2_PWDN_N (GPIO_ADC1_DRDY_N + 1)
95#define GPIO_EPM_SPI_ADC2_CS_N (GPIO_ADC2_PWDN_N + 1)
96#define GPIO_ADC2_DRDY_N (GPIO_EPM_SPI_ADC2_CS_N + 1)
97
98static int epm_adc_i2c_expander_register(void)
99{
100 int rc = 0;
101 static struct i2c_adapter *i2c_adap;
102 static struct i2c_client *epm_i2c_client;
103
104 rc = gpio_request(GPIO_EPM_GLOBAL_ENABLE, "EPM_GLOBAL_EN");
105 if (!rc) {
106 gpio_direction_output(GPIO_EPM_GLOBAL_ENABLE, 1);
107 } else {
108 pr_err("%s: Configure EPM_GLOBAL_EN Failed\n", __func__);
109 return rc;
110 }
111
112 usleep_range(EPM_ADC_CONVERSION_TIME_MIN,
113 EPM_ADC_CONVERSION_TIME_MAX);
114
115 i2c_adap = i2c_get_adapter(epm_adc_drv->bus_id);
116 if (i2c_adap == NULL) {
117 pr_err("%s: i2c_get_adapter() failed\n", __func__);
118 return -EINVAL;
119 }
120
121 usleep_range(EPM_ADC_CONVERSION_TIME_MIN,
122 EPM_ADC_CONVERSION_TIME_MAX);
123
124 epm_i2c_client = i2c_new_device(i2c_adap, epm_i2c_info);
125 if (IS_ERR(epm_i2c_client)) {
126 pr_err("Error with i2c epm device register\n");
127 return -ENODEV;
128 }
129
130 epm_adc_first_request = false;
131
132 return 0;
133}
134
135static int epm_adc_gpio_configure_expander_enable(void)
136{
137 int rc = 0;
138
139 if (epm_adc_first_request) {
140 rc = gpio_request(GPIO_EPM_GLOBAL_ENABLE, "EPM_GLOBAL_EN");
141 if (!rc) {
142 gpio_direction_output(GPIO_EPM_GLOBAL_ENABLE, 1);
143 } else {
144 pr_err("%s: Configure EPM_GLOBAL_EN Failed\n",
145 __func__);
146 return rc;
147 }
148 } else {
149 epm_adc_first_request = true;
150 }
151
152 usleep_range(EPM_ADC_CONVERSION_TIME_MIN,
153 EPM_ADC_CONVERSION_TIME_MAX);
154
155 rc = gpio_request(GPIO_PWR_MON_ENABLE, "GPIO_PWR_MON_ENABLE");
156 if (!rc) {
157 rc = gpio_direction_output(GPIO_PWR_MON_ENABLE, 1);
158 if (rc) {
159 pr_err("%s: Set GPIO_PWR_MON_ENABLE failed\n",
160 __func__);
161 return rc;
162 }
163 } else {
164 pr_err("%s: gpio_request GPIO_PWR_MON_ENABLE failed\n",
165 __func__);
166 return rc;
167 }
168
169 rc = gpio_request(GPIO_ADC1_PWDN_N, "GPIO_ADC1_PWDN_N");
170 if (!rc) {
171 rc = gpio_direction_output(GPIO_ADC1_PWDN_N, 1);
172 if (rc) {
173 pr_err("%s: Set GPIO_ADC1_PWDN_N failed\n", __func__);
174 return rc;
175 }
176 } else {
177 pr_err("%s: gpio_request GPIO_ADC1_PWDN_N failed\n", __func__);
178 return rc;
179 }
180
181 rc = gpio_request(GPIO_ADC2_PWDN_N, "GPIO_ADC2_PWDN_N");
182 if (!rc) {
183 rc = gpio_direction_output(GPIO_ADC2_PWDN_N, 1);
184 if (rc) {
185 pr_err("%s: Set GPIO_ADC2_PWDN_N failed\n",
186 __func__);
187 return rc;
188 }
189 } else {
190 pr_err("%s: gpio_request GPIO_ADC2_PWDN_N failed\n",
191 __func__);
192 return rc;
193 }
194
195 rc = gpio_request(GPIO_EPM_SPI_ADC1_CS_N, "GPIO_EPM_SPI_ADC1_CS_N");
196 if (!rc) {
197 rc = gpio_direction_output(GPIO_EPM_SPI_ADC1_CS_N, 1);
198 if (rc) {
199 pr_err("%s:Set GPIO_EPM_SPI_ADC1_CS_N failed\n",
200 __func__);
201 return rc;
202 }
203 } else {
204 pr_err("%s: gpio_request GPIO_EPM_SPI_ADC1_CS_N failed\n",
205 __func__);
206 return rc;
207 }
208
209 rc = gpio_request(GPIO_EPM_SPI_ADC2_CS_N,
210 "GPIO_EPM_SPI_ADC2_CS_N");
211 if (!rc) {
212 rc = gpio_direction_output(GPIO_EPM_SPI_ADC2_CS_N, 1);
213 if (rc) {
214 pr_err("%s: Set GPIO_EPM_SPI_ADC2_CS_N "
215 "failed\n", __func__);
216 return rc;
217 }
218 } else {
219 pr_err("%s: gpio_request GPIO_EPM_SPI_ADC2_CS_N "
220 "failed\n", __func__);
221 return rc;
222 }
223
224 rc = gpio_direction_output(GPIO_EPM_SPI_ADC1_CS_N, 0);
225 if (rc) {
226 pr_err("%s:Reset GPIO_EPM_SPI_ADC1_CS_N failed\n", __func__);
227 return rc;
228 }
229
230 rc = gpio_direction_output(GPIO_EPM_SPI_ADC1_CS_N, 1);
231 if (rc) {
232 pr_err("%s: Set GPIO_EPM_SPI_ADC1_CS_N failed\n", __func__);
233 return rc;
234 }
235
236 rc = gpio_request(GPIO_PWR_MON_START, "GPIO_PWR_MON_START");
237 if (!rc) {
238 rc = gpio_direction_output(GPIO_PWR_MON_START, 0);
239 if (rc) {
240 pr_err("%s: Reset GPIO_PWR_MON_START failed\n",
241 __func__);
242 return rc;
243 }
244 } else {
245 pr_err("%s: gpio_request GPIO_PWR_MON_START failed\n",
246 __func__);
247 return rc;
248 }
249
250 rc = gpio_request(GPIO_PWR_MON_RESET_N, "GPIO_PWR_MON_RESET_N");
251 if (!rc) {
252 rc = gpio_direction_output(GPIO_PWR_MON_RESET_N, 0);
253 if (rc) {
254 pr_err("%s: Reset GPIO_PWR_MON_RESET_N failed\n",
255 __func__);
256 return rc;
257 }
258 } else {
259 pr_err("%s: gpio_request GPIO_PWR_MON_RESET_N failed\n",
260 __func__);
261 return rc;
262 }
263
264 rc = gpio_direction_output(GPIO_PWR_MON_RESET_N, 1);
265 if (rc) {
266 pr_err("%s: Set GPIO_PWR_MON_RESET_N failed\n", __func__);
267 return rc;
268 }
269
270 rc = gpio_direction_output(GPIO_EPM_SPI_ADC1_CS_N, 0);
271 if (rc) {
272 pr_err("%s:Reset GPIO_EPM_SPI_ADC1_CS_N failed\n", __func__);
273 return rc;
274 }
275 return rc;
276}
277
278static int epm_adc_gpio_configure_expander_disable(void)
279{
280 int rc = 0;
281 gpio_free(GPIO_PWR_MON_ENABLE);
282 gpio_free(GPIO_ADC1_PWDN_N);
283 gpio_free(GPIO_ADC2_PWDN_N);
284 gpio_free(GPIO_EPM_SPI_ADC1_CS_N);
285 gpio_free(GPIO_EPM_SPI_ADC2_CS_N);
286 gpio_free(GPIO_PWR_MON_START);
287 gpio_free(GPIO_PWR_MON_RESET_N);
288 rc = gpio_direction_output(GPIO_EPM_GLOBAL_ENABLE, 0);
289 if (rc)
290 pr_debug("%s: Disable EPM_GLOBAL_EN Failed\n", __func__);
291 gpio_free(GPIO_EPM_GLOBAL_ENABLE);
292 return rc;
293}
294
295static int epm_adc_spi_chip_select(int32_t id)
296{
297 int rc = 0;
298 if (id == 0) {
299 rc = gpio_direction_output(GPIO_EPM_SPI_ADC2_CS_N, 1);
300 if (rc) {
301 pr_err("%s:Disable SPI_ADC2_CS failed",
302 __func__);
303 return rc;
304 }
305
306 rc = gpio_direction_output(GPIO_EPM_SPI_ADC1_CS_N, 0);
307 if (rc) {
308 pr_err("%s:Enable SPI_ADC1_CS failed", __func__);
309 return rc;
310 }
311 } else if (id == 1) {
312 rc = gpio_direction_output(GPIO_EPM_SPI_ADC1_CS_N, 1);
313 if (rc) {
314 pr_err("%s:Disable SPI_ADC1_CS failed", __func__);
315 return rc;
316 }
317 rc = gpio_direction_output(GPIO_EPM_SPI_ADC2_CS_N, 0);
318 if (rc) {
319 pr_err("%s:Enable SPI_ADC2_CS failed", __func__);
320 return rc;
321 }
322 } else {
323 rc = -EFAULT;
324 }
325 return rc;
326}
327
328static int epm_adc_ads_spi_write(struct epm_adc_drv *epm_adc,
329 uint8_t addr, uint8_t val)
330{
331 struct spi_message m;
332 struct spi_transfer t;
333 char tx_buf[2];
334 int rc = 0;
335
336 spi_setup(epm_adc->epm_spi_client);
337
338 memset(&t, 0, sizeof t);
339 memset(tx_buf, 0, sizeof tx_buf);
340 t.tx_buf = tx_buf;
341 spi_message_init(&m);
342 spi_message_add_tail(&t, &m);
343
344 tx_buf[0] = EPM_ADC_ADS_REG_WRITE_CMD | addr;
345 tx_buf[1] = val;
346
347 t.len = sizeof(tx_buf);
348 t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD;
349
350 rc = spi_sync(epm_adc->epm_spi_client, &m);
351
352 return rc;
353}
354
355static int epm_adc_init_ads(struct epm_adc_drv *epm_adc)
356{
357 int rc = 0;
358
359 rc = epm_adc_ads_spi_write(epm_adc, EPM_ADC_ADS_CONFIG0_REG_ADDR,
360 EPM_ADC_ADS_REG0_DEFAULT);
361 if (rc)
362 return rc;
363
364 rc = epm_adc_ads_spi_write(epm_adc, EPM_ADC_ADS_CONFIG1_REG_ADDR,
365 EPM_ADC_ADS_REG1_DEFAULT);
366 if (rc)
367 return rc;
368 return rc;
369}
370
371static int epm_adc_ads_pulse_convert(struct epm_adc_drv *epm_adc)
372{
373 struct spi_message m;
374 struct spi_transfer t;
375 char tx_buf[1];
376 int rc = 0;
377
378 spi_setup(epm_adc->epm_spi_client);
379
380 memset(&t, 0, sizeof t);
381 memset(tx_buf, 0, sizeof tx_buf);
382 t.tx_buf = tx_buf;
383 spi_message_init(&m);
384 spi_message_add_tail(&t, &m);
385
386 tx_buf[0] = EPM_ADC_ADS_PULSE_CONVERT_CMD;
387 t.len = sizeof(tx_buf);
388 t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD;
389
390 rc = spi_sync(epm_adc->epm_spi_client, &m);
391
392 return rc;
393}
394
395static int epm_adc_ads_read_data(struct epm_adc_drv *epm_adc, char *adc_data)
396{
397 struct spi_message m;
398 struct spi_transfer t;
399 char tx_buf[4], rx_buf[4];
400 int rc = 0;
401
402 spi_setup(epm_adc->epm_spi_client);
403
404 memset(&t, 0, sizeof t);
405 memset(tx_buf, 0, sizeof tx_buf);
406 memset(rx_buf, 0, sizeof tx_buf);
407 t.tx_buf = tx_buf;
408 t.rx_buf = rx_buf;
409 spi_message_init(&m);
410 spi_message_add_tail(&t, &m);
411
412 tx_buf[0] = EPM_ADC_ADS_DATA_READ_CMD |
413 EPM_ADC_ADS_MULTIPLE_REG_ACCESS;
414
415 t.len = sizeof(tx_buf);
416 t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD;
417
418 rc = spi_sync(epm_adc->epm_spi_client, &m);
419 if (rc)
420 return rc;
421
422 rc = spi_sync(epm_adc->epm_spi_client, &m);
423 if (rc)
424 return rc;
425
426 rc = spi_sync(epm_adc->epm_spi_client, &m);
427 if (rc)
428 return rc;
429
430 adc_data[0] = rx_buf[1];
431 adc_data[1] = rx_buf[2];
432 adc_data[2] = rx_buf[3];
433
434 return rc;
435}
436
437static int epm_adc_hw_init(struct epm_adc_drv *epm_adc)
438{
439 int rc = 0;
440
441 mutex_lock(&epm_adc->conv_lock);
442 rc = epm_adc_gpio_configure_expander_enable();
443 if (rc != 0) {
444 pr_err("epm gpio configure expander failed, rc = %d\n", rc);
445 goto epm_adc_hw_init_err;
446 }
447 rc = epm_adc_init_ads(epm_adc);
448 if (rc) {
449 pr_err("epm_adc_init_ads failed, rc=%d\n", rc);
450 goto epm_adc_hw_init_err;
451 }
452
453epm_adc_hw_init_err:
454 mutex_unlock(&epm_adc->conv_lock);
455 return rc;
456}
457
458static int epm_adc_hw_deinit(struct epm_adc_drv *epm_adc)
459{
460 int rc = 0;
461
462 mutex_lock(&epm_adc->conv_lock);
463 rc = epm_adc_gpio_configure_expander_disable();
464 if (rc != 0) {
465 pr_err("epm gpio configure expander disable failed,"
466 " rc = %d\n", rc);
467 goto epm_adc_hw_deinit_err;
468 }
469
470epm_adc_hw_deinit_err:
471 mutex_unlock(&epm_adc->conv_lock);
472 return rc;
473}
474
475static int epm_adc_ads_scale_result(struct epm_adc_drv *epm_adc,
476 uint8_t *adc_raw_data, struct epm_chan_request *conv)
477{
478 uint32_t channel_num;
479 int16_t sign_bit;
480 struct epm_adc_platform_data *pdata = epm_adc->pdev->dev.platform_data;
481 uint32_t chan_idx = (conv->device_idx * pdata->chan_per_adc) +
482 conv->channel_idx;
483 int32_t *adc_scaled_data = &conv->physical;
484
485 /* Get the channel number */
486 channel_num = (adc_raw_data[0] & EPM_ADC_ADS_CHANNEL_DATA_CHID);
487 sign_bit = 1;
488 /* This is the 16-bit raw data */
489 *adc_scaled_data = ((adc_raw_data[1] << 8) | adc_raw_data[2]);
490 /* Obtain the internal system reading */
491 if (channel_num == EPM_ADC_ADS_CHANNEL_VCC) {
492 *adc_scaled_data *= EPM_ADC_SCALE_MILLI;
493 *adc_scaled_data /= EPM_ADC_SCALE_CODE_VOLTS;
494 } else if (channel_num == EPM_ADC_ADS_CHANNEL_GAIN) {
495 *adc_scaled_data /= EPM_ADC_SCALE_CODE_GAIN;
496 } else if (channel_num == EPM_ADC_ADS_CHANNEL_REF) {
497 *adc_scaled_data *= EPM_ADC_SCALE_MILLI;
498 *adc_scaled_data /= EPM_ADC_SCALE_CODE_VOLTS;
499 } else if (channel_num == EPM_ADC_ADS_CHANNEL_TEMP) {
500 /* Convert Code to micro-volts */
501 /* Use this formula to get the temperature reading */
502 *adc_scaled_data -= EPM_ADC_TEMP_TO_DEGC_COEFF;
503 *adc_scaled_data /= EPM_ADC_TEMP_SENSOR_COEFF;
504 } else if (channel_num == EPM_ADC_ADS_CHANNEL_OFFSET) {
505 /* The offset should be zero */
506 pr_debug("%s: ADC Channel Offset\n", __func__);
507 return -EFAULT;
508 } else {
509 channel_num -= EPM_ADC_CHANNEL_AIN_OFFSET;
510 /*
511 * Conversion for the adc channels.
512 * mvVRef is in milli-volts and resistorValue is in micro-ohms.
513 * Hence, I = V/R gives us current in kilo-amps.
514 */
515 if (*adc_scaled_data & EPM_ADC_MAX_NEGATIVE_SCALE_CODE) {
516 sign_bit = -1;
517 *adc_scaled_data = (~*adc_scaled_data
518 & EPM_ADC_NEG_LSB_CODE);
519 }
520 if (*adc_scaled_data != 0) {
521 *adc_scaled_data *= EPM_ADC_SCALE_FACTOR;
522 /* Device is calibrated for 1LSB = VREF/7800h.*/
523 *adc_scaled_data *= EPM_ADC_MILLI_VOLTS_SOURCE;
524 *adc_scaled_data /= EPM_ADC_VREF_CODE;
525 /* Data will now be in micro-volts.*/
526 *adc_scaled_data *= EPM_ADC_SCALE_MILLI;
527 /* Divide by amplifier gain value.*/
528 *adc_scaled_data /= pdata->channel[chan_idx].gain;
529 /* Data will now be in nano-volts.*/
530 *adc_scaled_data /= EPM_ADC_SCALE_FACTOR;
531 *adc_scaled_data *= EPM_ADC_SCALE_MILLI;
532 /* Data is now in micro-amps.*/
533 *adc_scaled_data /=
534 pdata->channel[chan_idx].resistorValue;
535 /* Set the sign bit for lekage current. */
536 *adc_scaled_data *= sign_bit;
537 }
538 }
539 return 0;
540}
541
542static int epm_adc_blocking_conversion(struct epm_adc_drv *epm_adc,
543 struct epm_chan_request *conv)
544{
545 struct epm_adc_platform_data *pdata = epm_adc->pdev->dev.platform_data;
546 int32_t channel_num = 0, mux_chan_idx = 0;
547 char adc_data[3];
548 int rc = 0;
549
550 mutex_lock(&epm_adc->conv_lock);
551
552 rc = epm_adc_spi_chip_select(conv->device_idx);
553 if (rc) {
554 pr_err("epm_adc_chip_select failed, rc=%d\n", rc);
555 goto conv_err;
556 }
557
558 if (conv->channel_idx < pdata->chan_per_mux) {
559 /* Reset MUXSG1_REGISTER */
560 rc = epm_adc_ads_spi_write(epm_adc, EPM_ADC_ADS_MUXSG1_REG_ADDR,
561 0x0);
562 if (rc)
563 goto conv_err;
564
565 mux_chan_idx = 1 << conv->channel_idx;
566 /* Select Channel index in MUXSG0_REGISTER */
567 rc = epm_adc_ads_spi_write(epm_adc, EPM_ADC_ADS_MUXSG0_REG_ADDR,
568 mux_chan_idx);
569 if (rc)
570 goto conv_err;
571 } else {
572 /* Reset MUXSG0_REGISTER */
573 rc = epm_adc_ads_spi_write(epm_adc, EPM_ADC_ADS_MUXSG0_REG_ADDR,
574 0x0);
575 if (rc)
576 goto conv_err;
577
578 mux_chan_idx = 1 << (conv->channel_idx - pdata->chan_per_mux);
579 /* Select Channel index in MUXSG1_REGISTER */
580 rc = epm_adc_ads_spi_write(epm_adc, EPM_ADC_ADS_MUXSG1_REG_ADDR,
581 mux_chan_idx);
582 if (rc)
583 goto conv_err;
584 }
585
586 rc = epm_adc_ads_pulse_convert(epm_adc);
587 if (rc) {
588 pr_err("epm_adc_ads_pulse_convert failed, rc=%d\n", rc);
589 goto conv_err;
590 }
591
592 rc = epm_adc_ads_read_data(epm_adc, adc_data);
593 if (rc) {
594 pr_err("epm_adc_ads_read_data failed, rc=%d\n", rc);
595 goto conv_err;
596 }
597
598 channel_num = (adc_data[0] & EPM_ADC_ADS_CHANNEL_DATA_CHID);
599 pr_debug("ADC data Read: adc_data =%d, %d, %d\n",
600 adc_data[0], adc_data[1], adc_data[2]);
601
602 epm_adc_ads_scale_result(epm_adc, (uint8_t *)adc_data, conv);
603
604 pr_debug("channel_num(0x) = %x, scaled_data = %d\n",
605 (channel_num - EPM_ADC_ADS_SPI_BITS_PER_WORD),
606 conv->physical);
607conv_err:
608 mutex_unlock(&epm_adc->conv_lock);
609 return rc;
610}
611
Siddartha Mohanadosseb90c922012-03-27 22:58:06 -0700612static long epm_adc_ioctl(struct file *file, unsigned int cmd,
613 unsigned long arg)
614{
615 struct epm_adc_drv *epm_adc = epm_adc_drv;
616
617 switch (cmd) {
618 case EPM_ADC_REQUEST:
619 {
620 struct epm_chan_request conv;
621 int rc;
622
623 if (copy_from_user(&conv, (void __user *)arg,
624 sizeof(struct epm_chan_request)))
625 return -EFAULT;
626
627 rc = epm_adc_blocking_conversion(epm_adc, &conv);
628 if (rc) {
629 pr_err("Failed EPM conversion:%d\n", rc);
630 return rc;
631 }
632
633 if (copy_to_user((void __user *)arg, &conv,
634 sizeof(struct epm_chan_request)))
635 return -EFAULT;
636 break;
637 }
638 case EPM_ADC_INIT:
639 {
640 uint32_t result;
641 if (!epm_adc_expander_register) {
642 result = epm_adc_i2c_expander_register();
643 if (result) {
644 pr_err("Failed i2c register:%d\n",
645 result);
646 return result;
647 }
648 epm_adc_expander_register = true;
649 }
650
651 result = epm_adc_hw_init(epm_adc_drv);
652
653 if (copy_to_user((void __user *)arg, &result,
654 sizeof(uint32_t)))
655 return -EFAULT;
656 break;
657 }
658 case EPM_ADC_DEINIT:
659 {
660 uint32_t result;
661 result = epm_adc_hw_deinit(epm_adc_drv);
662
663 if (copy_to_user((void __user *)arg, &result,
664 sizeof(uint32_t)))
665 return -EFAULT;
666 break;
667 }
668 default:
669 return -EINVAL;
670 }
671
672 return 0;
673}
674
675const struct file_operations epm_adc_fops = {
676 .unlocked_ioctl = epm_adc_ioctl,
677};
678
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700679static ssize_t epm_adc_show_in(struct device *dev,
680 struct device_attribute *devattr, char *buf)
681{
682 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
683 struct epm_adc_drv *epm_adc = dev_get_drvdata(dev);
684 struct epm_adc_platform_data *pdata = epm_adc->pdev->dev.platform_data;
685 struct epm_chan_request conv;
686 int rc = 0;
687
688 conv.device_idx = attr->index / pdata->chan_per_adc;
689 conv.channel_idx = attr->index % pdata->chan_per_adc;
690 conv.physical = 0;
691 pr_debug("%s: device_idx=%d channel_idx=%d", __func__, conv.device_idx,
692 conv.channel_idx);
Siddartha Mohanadosseb90c922012-03-27 22:58:06 -0700693 if (!epm_adc_expander_register) {
694 rc = epm_adc_i2c_expander_register();
695 if (rc) {
696 pr_err("I2C expander register failed:%d\n", rc);
697 return rc;
698 }
699 epm_adc_expander_register = true;
700 }
701
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700702 rc = epm_adc_hw_init(epm_adc);
703 if (rc) {
704 pr_err("%s: epm_adc_hw_init() failed, rc = %d",
705 __func__, rc);
706 return 0;
707 }
708
709 rc = epm_adc_blocking_conversion(epm_adc, &conv);
710 if (rc) {
711 pr_err("%s: epm_adc_blocking_conversion() failed, rc = %d\n",
712 __func__, rc);
713 return 0;
714 }
715 rc = epm_adc_hw_deinit(epm_adc);
716 if (rc) {
717 pr_err("%s: epm_adc_hw_deinit() failed, rc = %d",
718 __func__, rc);
719 return 0;
720 }
721
722 return snprintf(buf, 16, "Result: %d\n", conv.physical);
723}
724
725static struct sensor_device_attribute epm_adc_in_attr =
726 SENSOR_ATTR(NULL, S_IRUGO, epm_adc_show_in, NULL, 0);
727
728static int __devinit epm_adc_init_hwmon(struct platform_device *pdev,
729 struct epm_adc_drv *epm_adc)
730{
731 struct epm_adc_platform_data *pdata = pdev->dev.platform_data;
732 int num_chans = pdata->num_channels, dev_idx = 0, chan_idx = 0;
733 int i = 0, rc = 0;
734 const char prefix[] = "ads", postfix[] = "_chan";
735 char tmpbuf[3];
736
737 epm_adc->fnames = devm_kzalloc(&pdev->dev,
738 num_chans * EPM_ADC_MAX_FNAME +
739 num_chans * sizeof(char *), GFP_KERNEL);
740 if (!epm_adc->fnames) {
741 dev_err(&pdev->dev, "Unable to allocate memory\n");
742 return -ENOMEM;
743 }
744
745 epm_adc->sens_attr = devm_kzalloc(&pdev->dev, num_chans *
746 sizeof(struct sensor_device_attribute), GFP_KERNEL);
747 if (!epm_adc->sens_attr) {
748 dev_err(&pdev->dev, "Unable to allocate memory\n");
749 rc = -ENOMEM;
750 }
751
752 for (i = 0; i < num_chans; i++, chan_idx++) {
753 epm_adc->fnames[i] = (char *)epm_adc->fnames +
754 (i * EPM_ADC_MAX_FNAME) + (num_chans *
755 sizeof(char *));
756 if (chan_idx == pdata->chan_per_adc) {
757 chan_idx = 0;
758 dev_idx++;
759 }
760 strlcpy(epm_adc->fnames[i], prefix, EPM_ADC_MAX_FNAME);
761 snprintf(tmpbuf, sizeof(tmpbuf), "%d", dev_idx);
762 strlcat(epm_adc->fnames[i], tmpbuf, EPM_ADC_MAX_FNAME);
763 strlcat(epm_adc->fnames[i], postfix, EPM_ADC_MAX_FNAME);
764 snprintf(tmpbuf, sizeof(tmpbuf), "%d", chan_idx);
765 strlcat(epm_adc->fnames[i], tmpbuf, EPM_ADC_MAX_FNAME);
766 epm_adc_in_attr.index = i;
767 epm_adc_in_attr.dev_attr.attr.name = epm_adc->fnames[i];
768 memcpy(&epm_adc->sens_attr[i], &epm_adc_in_attr,
769 sizeof(epm_adc_in_attr));
770 rc = device_create_file(&pdev->dev,
771 &epm_adc->sens_attr[i].dev_attr);
772 if (rc) {
773 dev_err(&pdev->dev, "device_create_file failed\n");
774 return rc;
775 }
776 }
777
778 return rc;
779}
780
781static int __devinit epm_adc_spi_probe(struct spi_device *spi)
782
783{
784 if (!epm_adc_drv)
785 return -ENODEV;
786 epm_adc_drv->epm_spi_client = spi;
787 epm_adc_drv->epm_spi_client->bits_per_word =
788 EPM_ADC_ADS_SPI_BITS_PER_WORD;
789
790 return 0;
791}
792
793static int __devexit epm_adc_spi_remove(struct spi_device *spi)
794{
795 epm_adc_drv->epm_spi_client = NULL;
796 return 0;
797}
798
799static struct spi_driver epm_spi_driver = {
800 .probe = epm_adc_spi_probe,
801 .remove = __devexit_p(epm_adc_spi_remove),
802 .driver = {
803 .name = EPM_ADC_DRIVER_NAME,
804 .owner = THIS_MODULE,
805 },
806};
807
Stephen Boyd58701e82012-04-25 11:48:28 -0700808static int __devinit epm_adc_probe(struct platform_device *pdev)
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700809{
810 struct epm_adc_drv *epm_adc;
811 struct epm_adc_platform_data *pdata = pdev->dev.platform_data;
812 int rc = 0;
813
814 if (!pdata) {
815 dev_err(&pdev->dev, "no platform data?\n");
816 return -EINVAL;
817 }
818
819 epm_adc = kzalloc(sizeof(struct epm_adc_drv), GFP_KERNEL);
820 if (!epm_adc) {
821 dev_err(&pdev->dev, "Unable to allocate memory\n");
822 return -ENOMEM;
823 }
824
825 platform_set_drvdata(pdev, epm_adc);
826 epm_adc_drv = epm_adc;
827 epm_adc->pdev = pdev;
828
Siddartha Mohanadosseb90c922012-03-27 22:58:06 -0700829 epm_adc->misc.name = EPM_ADC_DRIVER_NAME;
830 epm_adc->misc.minor = MISC_DYNAMIC_MINOR;
831 epm_adc->misc.fops = &epm_adc_fops;
832
833 if (misc_register(&epm_adc->misc)) {
834 dev_err(&pdev->dev, "Unable to register misc device!\n");
835 return -EFAULT;
836 }
837
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700838 rc = epm_adc_init_hwmon(pdev, epm_adc);
839 if (rc) {
840 dev_err(&pdev->dev, "msm_adc_dev_init failed\n");
Siddartha Mohanadosseb90c922012-03-27 22:58:06 -0700841 misc_deregister(&epm_adc->misc);
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700842 return rc;
843 }
844
845 epm_adc->hwmon = hwmon_device_register(&pdev->dev);
846 if (IS_ERR(epm_adc->hwmon)) {
847 dev_err(&pdev->dev, "hwmon_device_register failed\n");
Siddartha Mohanadosseb90c922012-03-27 22:58:06 -0700848 misc_deregister(&epm_adc->misc);
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700849 rc = PTR_ERR(epm_adc->hwmon);
850 return rc;
851 }
852
853 mutex_init(&epm_adc->conv_lock);
854 epm_i2c_info = &pdata->epm_i2c_board_info;
855 epm_adc->bus_id = pdata->bus_id;
856 epm_gpio_expander_base_addr = pdata->gpio_expander_base_addr;
Siddartha Mohanadosseb90c922012-03-27 22:58:06 -0700857 epm_adc_expander_register = false;
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700858 return rc;
859}
860
861static int __devexit epm_adc_remove(struct platform_device *pdev)
862{
863 struct epm_adc_drv *epm_adc = platform_get_drvdata(pdev);
864 struct epm_adc_platform_data *pdata = pdev->dev.platform_data;
865 int num_chans = pdata->num_channels;
866 int i = 0;
867
868 if (epm_adc->sens_attr)
869 for (i = 0; i < num_chans; i++)
870 device_remove_file(&pdev->dev,
871 &epm_adc->sens_attr[i].dev_attr);
872 hwmon_device_unregister(epm_adc->hwmon);
Siddartha Mohanadosseb90c922012-03-27 22:58:06 -0700873 misc_deregister(&epm_adc->misc);
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700874 epm_adc = NULL;
875
876 return 0;
877}
878
879static struct platform_driver epm_adc_driver = {
880 .probe = epm_adc_probe,
881 .remove = __devexit_p(epm_adc_remove),
882 .driver = {
883 .name = EPM_ADC_DRIVER_NAME,
884 .owner = THIS_MODULE,
885 },
886};
887
888static int __init epm_adc_init(void)
889{
890 int ret = 0;
891
892 ret = platform_driver_register(&epm_adc_driver);
893 if (ret) {
894 pr_err("%s: driver register failed, rc=%d\n", __func__, ret);
895 return ret;
896 }
897
898 ret = spi_register_driver(&epm_spi_driver);
899 if (ret)
900 pr_err("%s: spi register failed: rc=%d\n", __func__, ret);
901
902 return ret;
903}
904
905static void __exit epm_adc_exit(void)
906{
907 spi_unregister_driver(&epm_spi_driver);
908 platform_driver_unregister(&epm_adc_driver);
909}
910
911module_init(epm_adc_init);
912module_exit(epm_adc_exit);
913
914MODULE_DESCRIPTION("EPM ADC Driver");
915MODULE_ALIAS("platform:epm_adc");
916MODULE_LICENSE("GPL v2");