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