blob: 1b636252edc8f05b89eb04559b3fdcc69250bae7 [file] [log] [blame]
Duy Truonge833aca2013-02-12 13:35:08 -08001/* Copyright (c) 2012, The Linux Foundation. All rights reserved.
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -07002 *
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
Siddartha Mohanadossb0b8a6e2012-09-04 08:23:43 -070070/* PSoc Commands */
71#define EPM_PSOC_INIT_CMD 0x1
72#define EPM_PSOC_INIT_RESPONSE_CMD 0x2
73#define EPM_PSOC_CHANNEL_ENABLE_DISABLE_CMD 0x5
74#define EPM_PSOC_CHANNEL_ENABLE_DISABLE_RESPONSE_CMD 0x6
75#define EPM_PSOC_SET_AVERAGING_CMD 0x7
76#define EPM_PSOC_SET_AVERAGING_RESPONSE_CMD 0x8
77#define EPM_PSOC_GET_LAST_MEASUREMENT_CMD 0x9
78#define EPM_PSOC_GET_LAST_MEASUREMENT_RESPONSE_CMD 0xa
79#define EPM_PSOC_GET_BUFFERED_DATA_CMD 0xb
80#define EPM_PSOC_GET_BUFFERED_RESPONSE_CMD 0xc
81#define EPM_PSOC_GET_SYSTEM_TIMESTAMP_CMD 0x11
82#define EPM_PSOC_GET_SYSTEM_TIMESTAMP_RESPONSE_CMD 0x12
83#define EPM_PSOC_SET_SYSTEM_TIMESTAMP_CMD 0x13
84#define EPM_PSOC_SET_SYSTEM_TIMESTAMP_RESPONSE_CMD 0x14
85#define EPM_PSOC_SET_CHANNEL_TYPE_CMD 0x15
86#define EPM_PSOC_SET_CHANNEL_TYPE_RESPONSE_CMD 0x16
87#define EPM_PSOC_GET_AVERAGED_DATA_CMD 0x19
88#define EPM_PSOC_GET_AVERAGED_DATA_RESPONSE_CMD 0x1a
89#define EPM_PSOC_SET_CHANNEL_SWITCH_DELAY_CMD 0x1b
90#define EPM_PSOC_SET_CHANNEL_SWITCH_DELAY_RESPONSE_CMD 0x1c
91#define EPM_PSOC_CLEAR_BUFFER_CMD 0x1d
92#define EPM_PSOC_CLEAR_BUFFER_RESPONSE_CMD 0x1e
93#define EPM_PSOC_SET_VADC_REFERENCE_CMD 0x1f
94#define EPM_PSOC_SET_VADC_REFERENCE_RESPONSE_CMD 0x20
95
96#define EPM_PSOC_GLOBAL_ENABLE 81
97#define EPM_PSOC_VREF_VOLTAGE 2048
98#define EPM_PSOC_MAX_ADC_CODE_16_BIT 32767
99#define EPM_GLOBAL_ENABLE_MIN_DELAY 5000
100#define EPM_GLOBAL_ENABLE_MAX_DELAY 5100
101
102#define EPM_PSOC_BUFFERED_DATA_LENGTH 48
103#define EPM_PSOC_BUFFERED_DATA_LENGTH2 54
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700104
105struct epm_adc_drv {
106 struct platform_device *pdev;
107 struct device *hwmon;
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700108 struct spi_device *epm_spi_client;
109 struct mutex conv_lock;
110 uint32_t bus_id;
Siddartha Mohanadosseb90c922012-03-27 22:58:06 -0700111 struct miscdevice misc;
Siddartha Mohanadossb0b8a6e2012-09-04 08:23:43 -0700112 struct epm_chan_properties epm_psoc_ch_prop[0];
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700113};
114
115static struct epm_adc_drv *epm_adc_drv;
116static struct i2c_board_info *epm_i2c_info;
117static bool epm_adc_first_request;
118static int epm_gpio_expander_base_addr;
Siddartha Mohanadosseb90c922012-03-27 22:58:06 -0700119static bool epm_adc_expander_register;
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700120
121#define GPIO_EPM_EXPANDER_IO0 epm_gpio_expander_base_addr
122#define GPIO_PWR_MON_ENABLE (GPIO_EPM_EXPANDER_IO0 + 1)
123#define GPIO_ADC1_PWDN_N (GPIO_PWR_MON_ENABLE + 1)
124#define GPIO_PWR_MON_RESET_N (GPIO_ADC1_PWDN_N + 1)
125#define GPIO_EPM_SPI_ADC1_CS_N (GPIO_PWR_MON_RESET_N + 1)
126#define GPIO_PWR_MON_START (GPIO_EPM_SPI_ADC1_CS_N + 1)
127#define GPIO_ADC1_DRDY_N (GPIO_PWR_MON_START + 1)
128#define GPIO_ADC2_PWDN_N (GPIO_ADC1_DRDY_N + 1)
129#define GPIO_EPM_SPI_ADC2_CS_N (GPIO_ADC2_PWDN_N + 1)
130#define GPIO_ADC2_DRDY_N (GPIO_EPM_SPI_ADC2_CS_N + 1)
131
132static int epm_adc_i2c_expander_register(void)
133{
134 int rc = 0;
135 static struct i2c_adapter *i2c_adap;
136 static struct i2c_client *epm_i2c_client;
137
138 rc = gpio_request(GPIO_EPM_GLOBAL_ENABLE, "EPM_GLOBAL_EN");
139 if (!rc) {
140 gpio_direction_output(GPIO_EPM_GLOBAL_ENABLE, 1);
141 } else {
142 pr_err("%s: Configure EPM_GLOBAL_EN Failed\n", __func__);
143 return rc;
144 }
145
146 usleep_range(EPM_ADC_CONVERSION_TIME_MIN,
147 EPM_ADC_CONVERSION_TIME_MAX);
148
149 i2c_adap = i2c_get_adapter(epm_adc_drv->bus_id);
150 if (i2c_adap == NULL) {
151 pr_err("%s: i2c_get_adapter() failed\n", __func__);
152 return -EINVAL;
153 }
154
155 usleep_range(EPM_ADC_CONVERSION_TIME_MIN,
156 EPM_ADC_CONVERSION_TIME_MAX);
157
158 epm_i2c_client = i2c_new_device(i2c_adap, epm_i2c_info);
159 if (IS_ERR(epm_i2c_client)) {
160 pr_err("Error with i2c epm device register\n");
161 return -ENODEV;
162 }
163
164 epm_adc_first_request = false;
165
166 return 0;
167}
168
169static int epm_adc_gpio_configure_expander_enable(void)
170{
171 int rc = 0;
172
173 if (epm_adc_first_request) {
174 rc = gpio_request(GPIO_EPM_GLOBAL_ENABLE, "EPM_GLOBAL_EN");
175 if (!rc) {
176 gpio_direction_output(GPIO_EPM_GLOBAL_ENABLE, 1);
177 } else {
178 pr_err("%s: Configure EPM_GLOBAL_EN Failed\n",
179 __func__);
180 return rc;
181 }
182 } else {
183 epm_adc_first_request = true;
184 }
185
186 usleep_range(EPM_ADC_CONVERSION_TIME_MIN,
187 EPM_ADC_CONVERSION_TIME_MAX);
188
189 rc = gpio_request(GPIO_PWR_MON_ENABLE, "GPIO_PWR_MON_ENABLE");
190 if (!rc) {
191 rc = gpio_direction_output(GPIO_PWR_MON_ENABLE, 1);
192 if (rc) {
193 pr_err("%s: Set GPIO_PWR_MON_ENABLE failed\n",
194 __func__);
195 return rc;
196 }
197 } else {
198 pr_err("%s: gpio_request GPIO_PWR_MON_ENABLE failed\n",
199 __func__);
200 return rc;
201 }
202
203 rc = gpio_request(GPIO_ADC1_PWDN_N, "GPIO_ADC1_PWDN_N");
204 if (!rc) {
205 rc = gpio_direction_output(GPIO_ADC1_PWDN_N, 1);
206 if (rc) {
207 pr_err("%s: Set GPIO_ADC1_PWDN_N failed\n", __func__);
208 return rc;
209 }
210 } else {
211 pr_err("%s: gpio_request GPIO_ADC1_PWDN_N failed\n", __func__);
212 return rc;
213 }
214
215 rc = gpio_request(GPIO_ADC2_PWDN_N, "GPIO_ADC2_PWDN_N");
216 if (!rc) {
217 rc = gpio_direction_output(GPIO_ADC2_PWDN_N, 1);
218 if (rc) {
219 pr_err("%s: Set GPIO_ADC2_PWDN_N failed\n",
220 __func__);
221 return rc;
222 }
223 } else {
224 pr_err("%s: gpio_request GPIO_ADC2_PWDN_N failed\n",
225 __func__);
226 return rc;
227 }
228
229 rc = gpio_request(GPIO_EPM_SPI_ADC1_CS_N, "GPIO_EPM_SPI_ADC1_CS_N");
230 if (!rc) {
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",
234 __func__);
235 return rc;
236 }
237 } else {
238 pr_err("%s: gpio_request GPIO_EPM_SPI_ADC1_CS_N failed\n",
239 __func__);
240 return rc;
241 }
242
243 rc = gpio_request(GPIO_EPM_SPI_ADC2_CS_N,
244 "GPIO_EPM_SPI_ADC2_CS_N");
245 if (!rc) {
246 rc = gpio_direction_output(GPIO_EPM_SPI_ADC2_CS_N, 1);
247 if (rc) {
Siddartha Mohanadossb0b8a6e2012-09-04 08:23:43 -0700248 pr_err("Set GPIO_EPM_SPI_ADC2_CS_N failed\n");
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700249 return rc;
250 }
251 } else {
Siddartha Mohanadossb0b8a6e2012-09-04 08:23:43 -0700252 pr_err("gpio_request GPIO_EPM_SPI_ADC2_CS_N failed\n");
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700253 return rc;
254 }
255
256 rc = gpio_direction_output(GPIO_EPM_SPI_ADC1_CS_N, 0);
257 if (rc) {
258 pr_err("%s:Reset GPIO_EPM_SPI_ADC1_CS_N failed\n", __func__);
259 return rc;
260 }
261
262 rc = gpio_direction_output(GPIO_EPM_SPI_ADC1_CS_N, 1);
263 if (rc) {
264 pr_err("%s: Set GPIO_EPM_SPI_ADC1_CS_N failed\n", __func__);
265 return rc;
266 }
267
268 rc = gpio_request(GPIO_PWR_MON_START, "GPIO_PWR_MON_START");
269 if (!rc) {
270 rc = gpio_direction_output(GPIO_PWR_MON_START, 0);
271 if (rc) {
272 pr_err("%s: Reset GPIO_PWR_MON_START failed\n",
273 __func__);
274 return rc;
275 }
276 } else {
277 pr_err("%s: gpio_request GPIO_PWR_MON_START failed\n",
278 __func__);
279 return rc;
280 }
281
282 rc = gpio_request(GPIO_PWR_MON_RESET_N, "GPIO_PWR_MON_RESET_N");
283 if (!rc) {
284 rc = gpio_direction_output(GPIO_PWR_MON_RESET_N, 0);
285 if (rc) {
286 pr_err("%s: Reset GPIO_PWR_MON_RESET_N failed\n",
287 __func__);
288 return rc;
289 }
290 } else {
291 pr_err("%s: gpio_request GPIO_PWR_MON_RESET_N failed\n",
292 __func__);
293 return rc;
294 }
295
296 rc = gpio_direction_output(GPIO_PWR_MON_RESET_N, 1);
297 if (rc) {
298 pr_err("%s: Set GPIO_PWR_MON_RESET_N failed\n", __func__);
299 return rc;
300 }
301
302 rc = gpio_direction_output(GPIO_EPM_SPI_ADC1_CS_N, 0);
303 if (rc) {
304 pr_err("%s:Reset GPIO_EPM_SPI_ADC1_CS_N failed\n", __func__);
305 return rc;
306 }
307 return rc;
308}
309
310static int epm_adc_gpio_configure_expander_disable(void)
311{
312 int rc = 0;
313 gpio_free(GPIO_PWR_MON_ENABLE);
314 gpio_free(GPIO_ADC1_PWDN_N);
315 gpio_free(GPIO_ADC2_PWDN_N);
316 gpio_free(GPIO_EPM_SPI_ADC1_CS_N);
317 gpio_free(GPIO_EPM_SPI_ADC2_CS_N);
318 gpio_free(GPIO_PWR_MON_START);
319 gpio_free(GPIO_PWR_MON_RESET_N);
320 rc = gpio_direction_output(GPIO_EPM_GLOBAL_ENABLE, 0);
321 if (rc)
322 pr_debug("%s: Disable EPM_GLOBAL_EN Failed\n", __func__);
323 gpio_free(GPIO_EPM_GLOBAL_ENABLE);
324 return rc;
325}
326
327static int epm_adc_spi_chip_select(int32_t id)
328{
329 int rc = 0;
330 if (id == 0) {
331 rc = gpio_direction_output(GPIO_EPM_SPI_ADC2_CS_N, 1);
332 if (rc) {
333 pr_err("%s:Disable SPI_ADC2_CS failed",
334 __func__);
335 return rc;
336 }
337
338 rc = gpio_direction_output(GPIO_EPM_SPI_ADC1_CS_N, 0);
339 if (rc) {
340 pr_err("%s:Enable SPI_ADC1_CS failed", __func__);
341 return rc;
342 }
343 } else if (id == 1) {
344 rc = gpio_direction_output(GPIO_EPM_SPI_ADC1_CS_N, 1);
345 if (rc) {
346 pr_err("%s:Disable SPI_ADC1_CS failed", __func__);
347 return rc;
348 }
349 rc = gpio_direction_output(GPIO_EPM_SPI_ADC2_CS_N, 0);
350 if (rc) {
351 pr_err("%s:Enable SPI_ADC2_CS failed", __func__);
352 return rc;
353 }
354 } else {
355 rc = -EFAULT;
356 }
357 return rc;
358}
359
360static int epm_adc_ads_spi_write(struct epm_adc_drv *epm_adc,
361 uint8_t addr, uint8_t val)
362{
363 struct spi_message m;
364 struct spi_transfer t;
365 char tx_buf[2];
366 int rc = 0;
367
368 spi_setup(epm_adc->epm_spi_client);
369
370 memset(&t, 0, sizeof t);
371 memset(tx_buf, 0, sizeof tx_buf);
372 t.tx_buf = tx_buf;
373 spi_message_init(&m);
374 spi_message_add_tail(&t, &m);
375
376 tx_buf[0] = EPM_ADC_ADS_REG_WRITE_CMD | addr;
377 tx_buf[1] = val;
378
379 t.len = sizeof(tx_buf);
380 t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD;
381
382 rc = spi_sync(epm_adc->epm_spi_client, &m);
383
384 return rc;
385}
386
387static int epm_adc_init_ads(struct epm_adc_drv *epm_adc)
388{
389 int rc = 0;
390
391 rc = epm_adc_ads_spi_write(epm_adc, EPM_ADC_ADS_CONFIG0_REG_ADDR,
392 EPM_ADC_ADS_REG0_DEFAULT);
393 if (rc)
394 return rc;
395
396 rc = epm_adc_ads_spi_write(epm_adc, EPM_ADC_ADS_CONFIG1_REG_ADDR,
397 EPM_ADC_ADS_REG1_DEFAULT);
398 if (rc)
399 return rc;
400 return rc;
401}
402
403static int epm_adc_ads_pulse_convert(struct epm_adc_drv *epm_adc)
404{
405 struct spi_message m;
406 struct spi_transfer t;
407 char tx_buf[1];
408 int rc = 0;
409
410 spi_setup(epm_adc->epm_spi_client);
411
412 memset(&t, 0, sizeof t);
413 memset(tx_buf, 0, sizeof tx_buf);
414 t.tx_buf = tx_buf;
415 spi_message_init(&m);
416 spi_message_add_tail(&t, &m);
417
418 tx_buf[0] = EPM_ADC_ADS_PULSE_CONVERT_CMD;
419 t.len = sizeof(tx_buf);
420 t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD;
421
422 rc = spi_sync(epm_adc->epm_spi_client, &m);
423
424 return rc;
425}
426
427static int epm_adc_ads_read_data(struct epm_adc_drv *epm_adc, char *adc_data)
428{
429 struct spi_message m;
430 struct spi_transfer t;
431 char tx_buf[4], rx_buf[4];
432 int rc = 0;
433
434 spi_setup(epm_adc->epm_spi_client);
435
436 memset(&t, 0, sizeof t);
437 memset(tx_buf, 0, sizeof tx_buf);
438 memset(rx_buf, 0, sizeof tx_buf);
439 t.tx_buf = tx_buf;
440 t.rx_buf = rx_buf;
441 spi_message_init(&m);
442 spi_message_add_tail(&t, &m);
443
444 tx_buf[0] = EPM_ADC_ADS_DATA_READ_CMD |
445 EPM_ADC_ADS_MULTIPLE_REG_ACCESS;
446
447 t.len = sizeof(tx_buf);
448 t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD;
449
450 rc = spi_sync(epm_adc->epm_spi_client, &m);
451 if (rc)
452 return rc;
453
454 rc = spi_sync(epm_adc->epm_spi_client, &m);
455 if (rc)
456 return rc;
457
458 rc = spi_sync(epm_adc->epm_spi_client, &m);
459 if (rc)
460 return rc;
461
462 adc_data[0] = rx_buf[1];
463 adc_data[1] = rx_buf[2];
464 adc_data[2] = rx_buf[3];
465
466 return rc;
467}
468
469static int epm_adc_hw_init(struct epm_adc_drv *epm_adc)
470{
471 int rc = 0;
472
473 mutex_lock(&epm_adc->conv_lock);
474 rc = epm_adc_gpio_configure_expander_enable();
475 if (rc != 0) {
476 pr_err("epm gpio configure expander failed, rc = %d\n", rc);
477 goto epm_adc_hw_init_err;
478 }
479 rc = epm_adc_init_ads(epm_adc);
480 if (rc) {
481 pr_err("epm_adc_init_ads failed, rc=%d\n", rc);
482 goto epm_adc_hw_init_err;
483 }
484
485epm_adc_hw_init_err:
486 mutex_unlock(&epm_adc->conv_lock);
487 return rc;
488}
489
490static int epm_adc_hw_deinit(struct epm_adc_drv *epm_adc)
491{
492 int rc = 0;
493
494 mutex_lock(&epm_adc->conv_lock);
495 rc = epm_adc_gpio_configure_expander_disable();
496 if (rc != 0) {
Siddartha Mohanadossb0b8a6e2012-09-04 08:23:43 -0700497 pr_err("gpio expander disable failed with %d\n", rc);
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700498 goto epm_adc_hw_deinit_err;
499 }
500
501epm_adc_hw_deinit_err:
502 mutex_unlock(&epm_adc->conv_lock);
503 return rc;
504}
505
506static int epm_adc_ads_scale_result(struct epm_adc_drv *epm_adc,
507 uint8_t *adc_raw_data, struct epm_chan_request *conv)
508{
509 uint32_t channel_num;
510 int16_t sign_bit;
511 struct epm_adc_platform_data *pdata = epm_adc->pdev->dev.platform_data;
512 uint32_t chan_idx = (conv->device_idx * pdata->chan_per_adc) +
513 conv->channel_idx;
Siddartha Mohanadosscd7672f2012-09-18 16:57:50 -0700514 int64_t adc_scaled_data = 0;
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700515
516 /* Get the channel number */
517 channel_num = (adc_raw_data[0] & EPM_ADC_ADS_CHANNEL_DATA_CHID);
518 sign_bit = 1;
519 /* This is the 16-bit raw data */
Siddartha Mohanadosscd7672f2012-09-18 16:57:50 -0700520 adc_scaled_data = ((adc_raw_data[1] << 8) | adc_raw_data[2]);
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700521 /* Obtain the internal system reading */
522 if (channel_num == EPM_ADC_ADS_CHANNEL_VCC) {
Siddartha Mohanadosscd7672f2012-09-18 16:57:50 -0700523 adc_scaled_data *= EPM_ADC_SCALE_MILLI;
524 do_div(adc_scaled_data, EPM_ADC_SCALE_CODE_VOLTS);
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700525 } else if (channel_num == EPM_ADC_ADS_CHANNEL_GAIN) {
Siddartha Mohanadosscd7672f2012-09-18 16:57:50 -0700526 do_div(adc_scaled_data, EPM_ADC_SCALE_CODE_GAIN);
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700527 } else if (channel_num == EPM_ADC_ADS_CHANNEL_REF) {
Siddartha Mohanadosscd7672f2012-09-18 16:57:50 -0700528 adc_scaled_data *= EPM_ADC_SCALE_MILLI;
529 do_div(adc_scaled_data, EPM_ADC_SCALE_CODE_VOLTS);
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700530 } else if (channel_num == EPM_ADC_ADS_CHANNEL_TEMP) {
531 /* Convert Code to micro-volts */
532 /* Use this formula to get the temperature reading */
Siddartha Mohanadosscd7672f2012-09-18 16:57:50 -0700533 adc_scaled_data -= EPM_ADC_TEMP_TO_DEGC_COEFF;
534 do_div(adc_scaled_data, EPM_ADC_TEMP_SENSOR_COEFF);
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700535 } else if (channel_num == EPM_ADC_ADS_CHANNEL_OFFSET) {
536 /* The offset should be zero */
537 pr_debug("%s: ADC Channel Offset\n", __func__);
538 return -EFAULT;
539 } else {
540 channel_num -= EPM_ADC_CHANNEL_AIN_OFFSET;
541 /*
542 * Conversion for the adc channels.
Siddartha Mohanadossb0b8a6e2012-09-04 08:23:43 -0700543 * mvVRef is in milli-volts and resistorvalue is in micro-ohms.
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700544 * Hence, I = V/R gives us current in kilo-amps.
545 */
Siddartha Mohanadosscd7672f2012-09-18 16:57:50 -0700546 if (adc_scaled_data & EPM_ADC_MAX_NEGATIVE_SCALE_CODE) {
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700547 sign_bit = -1;
Siddartha Mohanadosscd7672f2012-09-18 16:57:50 -0700548 adc_scaled_data = (~adc_scaled_data
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700549 & EPM_ADC_NEG_LSB_CODE);
550 }
Siddartha Mohanadosscd7672f2012-09-18 16:57:50 -0700551 if (adc_scaled_data != 0) {
552 adc_scaled_data *= EPM_ADC_SCALE_FACTOR;
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700553 /* Device is calibrated for 1LSB = VREF/7800h.*/
Siddartha Mohanadosscd7672f2012-09-18 16:57:50 -0700554 adc_scaled_data *= EPM_ADC_MILLI_VOLTS_SOURCE;
555 do_div(adc_scaled_data, EPM_ADC_VREF_CODE);
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700556 /* Data will now be in micro-volts.*/
Siddartha Mohanadosscd7672f2012-09-18 16:57:50 -0700557 adc_scaled_data *= EPM_ADC_SCALE_MILLI;
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700558 /* Divide by amplifier gain value.*/
Siddartha Mohanadosscd7672f2012-09-18 16:57:50 -0700559 do_div(adc_scaled_data, pdata->channel[chan_idx].gain);
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700560 /* Data will now be in nano-volts.*/
Siddartha Mohanadosscd7672f2012-09-18 16:57:50 -0700561 do_div(adc_scaled_data, EPM_ADC_SCALE_FACTOR);
562 adc_scaled_data *= EPM_ADC_SCALE_MILLI;
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700563 /* Data is now in micro-amps.*/
Siddartha Mohanadosscd7672f2012-09-18 16:57:50 -0700564 do_div(adc_scaled_data,
Siddartha Mohanadossb0b8a6e2012-09-04 08:23:43 -0700565 pdata->channel[chan_idx].resistorvalue);
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700566 /* Set the sign bit for lekage current. */
Siddartha Mohanadosscd7672f2012-09-18 16:57:50 -0700567 adc_scaled_data *= sign_bit;
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700568 }
569 }
Siddartha Mohanadosscd7672f2012-09-18 16:57:50 -0700570 conv->physical = (int32_t) adc_scaled_data;
Yan He2af37f12012-08-31 11:09:36 -0700571
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700572 return 0;
573}
574
Siddartha Mohanadossb0b8a6e2012-09-04 08:23:43 -0700575static int epm_psoc_scale_result(uint16_t *adc_raw_data, uint32_t index)
576{
577 struct epm_adc_drv *epm_adc = epm_adc_drv;
578 /* result = 2.048V/(32767 * gain * rsense) */
579 *adc_raw_data = (EPM_PSOC_VREF_VOLTAGE/EPM_PSOC_MAX_ADC_CODE_16_BIT)
580 * (*adc_raw_data);
581 *adc_raw_data = *adc_raw_data/
582 (epm_adc->epm_psoc_ch_prop[index].gain *
583 epm_adc->epm_psoc_ch_prop[index].resistorvalue);
584 return 0;
585}
586
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700587static int epm_adc_blocking_conversion(struct epm_adc_drv *epm_adc,
588 struct epm_chan_request *conv)
589{
590 struct epm_adc_platform_data *pdata = epm_adc->pdev->dev.platform_data;
591 int32_t channel_num = 0, mux_chan_idx = 0;
592 char adc_data[3];
593 int rc = 0;
594
595 mutex_lock(&epm_adc->conv_lock);
596
597 rc = epm_adc_spi_chip_select(conv->device_idx);
598 if (rc) {
599 pr_err("epm_adc_chip_select failed, rc=%d\n", rc);
600 goto conv_err;
601 }
602
603 if (conv->channel_idx < pdata->chan_per_mux) {
604 /* Reset MUXSG1_REGISTER */
605 rc = epm_adc_ads_spi_write(epm_adc, EPM_ADC_ADS_MUXSG1_REG_ADDR,
606 0x0);
607 if (rc)
608 goto conv_err;
609
610 mux_chan_idx = 1 << conv->channel_idx;
611 /* Select Channel index in MUXSG0_REGISTER */
612 rc = epm_adc_ads_spi_write(epm_adc, EPM_ADC_ADS_MUXSG0_REG_ADDR,
613 mux_chan_idx);
614 if (rc)
615 goto conv_err;
616 } else {
617 /* Reset MUXSG0_REGISTER */
618 rc = epm_adc_ads_spi_write(epm_adc, EPM_ADC_ADS_MUXSG0_REG_ADDR,
619 0x0);
620 if (rc)
621 goto conv_err;
622
623 mux_chan_idx = 1 << (conv->channel_idx - pdata->chan_per_mux);
624 /* Select Channel index in MUXSG1_REGISTER */
625 rc = epm_adc_ads_spi_write(epm_adc, EPM_ADC_ADS_MUXSG1_REG_ADDR,
626 mux_chan_idx);
627 if (rc)
628 goto conv_err;
629 }
630
631 rc = epm_adc_ads_pulse_convert(epm_adc);
632 if (rc) {
633 pr_err("epm_adc_ads_pulse_convert failed, rc=%d\n", rc);
634 goto conv_err;
635 }
636
637 rc = epm_adc_ads_read_data(epm_adc, adc_data);
638 if (rc) {
639 pr_err("epm_adc_ads_read_data failed, rc=%d\n", rc);
640 goto conv_err;
641 }
642
643 channel_num = (adc_data[0] & EPM_ADC_ADS_CHANNEL_DATA_CHID);
644 pr_debug("ADC data Read: adc_data =%d, %d, %d\n",
645 adc_data[0], adc_data[1], adc_data[2]);
646
647 epm_adc_ads_scale_result(epm_adc, (uint8_t *)adc_data, conv);
648
649 pr_debug("channel_num(0x) = %x, scaled_data = %d\n",
650 (channel_num - EPM_ADC_ADS_SPI_BITS_PER_WORD),
651 conv->physical);
652conv_err:
653 mutex_unlock(&epm_adc->conv_lock);
654 return rc;
655}
656
Siddartha Mohanadossb0b8a6e2012-09-04 08:23:43 -0700657static int epm_adc_psoc_gpio_init(bool enable)
658{
659 int rc = 0;
660
661 if (enable) {
662 rc = gpio_request(EPM_PSOC_GLOBAL_ENABLE, "EPM_PSOC_GLOBAL_EN");
663 if (!rc) {
664 gpio_direction_output(EPM_PSOC_GLOBAL_ENABLE, 1);
665 } else {
666 pr_err("%s: Configure EPM_GLOBAL_EN Failed\n",
667 __func__);
668 return rc;
669 }
670 } else {
671 gpio_direction_output(EPM_PSOC_GLOBAL_ENABLE, 0);
672 gpio_free(EPM_PSOC_GLOBAL_ENABLE);
673 }
674
675 return 0;
676}
677
678static int epm_psoc_init(struct epm_adc_drv *epm_adc,
679 struct epm_psoc_init_resp *init_resp)
680{
681 struct spi_message m;
682 struct spi_transfer t;
683 char tx_buf[17], rx_buf[17];
684 int rc = 0;
685
686 spi_setup(epm_adc->epm_spi_client);
687
688 memset(&t, 0, sizeof t);
689 memset(tx_buf, 0, sizeof tx_buf);
690 memset(rx_buf, 0, sizeof tx_buf);
691 t.tx_buf = tx_buf;
692 t.rx_buf = rx_buf;
693 spi_message_init(&m);
694 spi_message_add_tail(&t, &m);
695
696 tx_buf[0] = init_resp->cmd;
697
698 t.len = sizeof(tx_buf);
699 t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD;
700
701 rc = spi_sync(epm_adc->epm_spi_client, &m);
702 if (rc)
703 return rc;
704
705 init_resp->cmd = rx_buf[0];
706 init_resp->version = rx_buf[1];
707 init_resp->compatible_ver = rx_buf[2];
708 init_resp->firm_ver[0] = rx_buf[3];
709 init_resp->firm_ver[1] = rx_buf[4];
710 init_resp->firm_ver[2] = rx_buf[5];
711 init_resp->num_dev = rx_buf[6];
712 init_resp->num_channel = rx_buf[7];
713
714 return rc;
715}
716
717static int epm_psoc_channel_configure(struct epm_adc_drv *epm_adc,
718 struct epm_psoc_channel_configure *psoc_chan_configure)
719{
720 struct spi_message m;
721 struct spi_transfer t;
722 char tx_buf[9], rx_buf[9];
723 int32_t rc = 0, chan_num;
724
725 spi_setup(epm_adc->epm_spi_client);
726
727 memset(&t, 0, sizeof t);
728 memset(tx_buf, 0, sizeof tx_buf);
729 memset(rx_buf, 0, sizeof tx_buf);
730 t.tx_buf = tx_buf;
731 t.rx_buf = rx_buf;
732 spi_message_init(&m);
733 spi_message_add_tail(&t, &m);
734
735 chan_num = psoc_chan_configure->channel_num;
736
737 tx_buf[0] = psoc_chan_configure->cmd;
738 tx_buf[1] = 0;
739 tx_buf[2] = (chan_num & 0xff000000) >> 24;
740 tx_buf[3] = (chan_num & 0xff0000) >> 16;
741 tx_buf[4] = (chan_num & 0xff00) >> 8;
742 tx_buf[5] = (chan_num & 0xff);
743
744 t.len = sizeof(tx_buf);
745 t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD;
746
747 rc = spi_sync(epm_adc->epm_spi_client, &m);
748 if (rc)
749 return rc;
750
751 rc = spi_sync(epm_adc->epm_spi_client, &m);
752 if (rc)
753 return rc;
754
755 psoc_chan_configure->cmd = rx_buf[0];
756 psoc_chan_configure->device_num = rx_buf[1];
757 chan_num = rx_buf[2] << 24 | (rx_buf[3] << 16) | (rx_buf[4] << 8) |
758 rx_buf[5];
759 psoc_chan_configure->channel_num = chan_num;
760 pr_debug("dev_num:%d, chan_num:%d\n", rx_buf[1], chan_num);
761
762 return rc;
763}
764
765static int epm_psoc_set_averaging(struct epm_adc_drv *epm_adc,
766 struct epm_psoc_set_avg *psoc_set_avg)
767{
768 struct spi_message m;
769 struct spi_transfer t;
770 char tx_buf[4], rx_buf[4];
771 int rc = 0;
772
773 spi_setup(epm_adc->epm_spi_client);
774
775 memset(&t, 0, sizeof t);
776 memset(tx_buf, 0, sizeof tx_buf);
777 memset(rx_buf, 0, sizeof tx_buf);
778 t.tx_buf = tx_buf;
779 t.rx_buf = rx_buf;
780 spi_message_init(&m);
781 spi_message_add_tail(&t, &m);
782
783 tx_buf[0] = psoc_set_avg->cmd;
784 tx_buf[1] = psoc_set_avg->avg_period;
785
786 t.len = sizeof(tx_buf);
787 t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD;
788
789 rc = spi_sync(epm_adc->epm_spi_client, &m);
790 if (rc)
791 return rc;
792
793 rc = spi_sync(epm_adc->epm_spi_client, &m);
794 if (rc)
795 return rc;
796
797 psoc_set_avg->cmd = rx_buf[0];
798 psoc_set_avg->return_code = rx_buf[1];
799
800 return rc;
801}
802
803static int epm_psoc_get_data(struct epm_adc_drv *epm_adc,
804 struct epm_psoc_get_data *psoc_get_meas)
805{
806 struct spi_message m;
807 struct spi_transfer t;
808 char tx_buf[10], rx_buf[10];
809 int rc = 0;
810
811 spi_setup(epm_adc->epm_spi_client);
812
813 memset(&t, 0, sizeof t);
814 memset(tx_buf, 0, sizeof tx_buf);
815 memset(rx_buf, 0, sizeof tx_buf);
816 t.tx_buf = tx_buf;
817 t.rx_buf = rx_buf;
818 spi_message_init(&m);
819 spi_message_add_tail(&t, &m);
820
821 tx_buf[0] = psoc_get_meas->cmd;
822 tx_buf[1] = psoc_get_meas->dev_num;
823 tx_buf[2] = psoc_get_meas->chan_num;
824
825 t.len = sizeof(tx_buf);
826 t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD;
827
828 rc = spi_sync(epm_adc->epm_spi_client, &m);
829 if (rc)
830 return rc;
831
832 rc = spi_sync(epm_adc->epm_spi_client, &m);
833 if (rc)
834 return rc;
835
836 psoc_get_meas->cmd = rx_buf[0];
837 psoc_get_meas->dev_num = rx_buf[1];
838 psoc_get_meas->chan_num = rx_buf[2];
839 psoc_get_meas->timestamp_resp_value = (rx_buf[3] << 24) |
840 (rx_buf[4] << 16) | (rx_buf[5] << 8) |
841 rx_buf[6];
842 psoc_get_meas->reading_value = (rx_buf[7] << 8) | rx_buf[8];
843
844 pr_debug("dev_num:%d, chan_num:%d\n", rx_buf[1], rx_buf[2]);
845 pr_debug("data %d\n", psoc_get_meas->reading_value);
846 return rc;
847}
848
849static int epm_psoc_get_buffered_data(struct epm_adc_drv *epm_adc,
850 struct epm_psoc_get_buffered_data *psoc_get_meas)
851{
852 struct spi_message m;
853 struct spi_transfer t;
854 char tx_buf[64], rx_buf[64];
855 int rc = 0, i;
856
857 spi_setup(epm_adc->epm_spi_client);
858
859 memset(&t, 0, sizeof t);
860 memset(tx_buf, 0, sizeof tx_buf);
861 memset(rx_buf, 0, sizeof tx_buf);
862 t.tx_buf = tx_buf;
863 t.rx_buf = rx_buf;
864 spi_message_init(&m);
865 spi_message_add_tail(&t, &m);
866
867 tx_buf[0] = psoc_get_meas->cmd;
868
869 t.len = sizeof(tx_buf);
870 t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD;
871
872 rc = spi_sync(epm_adc->epm_spi_client, &m);
873 if (rc)
874 return rc;
875
876 rc = spi_sync(epm_adc->epm_spi_client, &m);
877 if (rc)
878 return rc;
879
880 psoc_get_meas->cmd = rx_buf[0];
881 psoc_get_meas->dev_num = rx_buf[1];
882 psoc_get_meas->status_mask = rx_buf[2];
883 psoc_get_meas->chan_idx = rx_buf[3];
884 psoc_get_meas->chan_mask = (rx_buf[4] << 24 |
885 rx_buf[5] << 16 | rx_buf[6] << 8
886 | rx_buf[7]);
887 psoc_get_meas->timestamp_start = (rx_buf[8] << 24 |
888 rx_buf[9] << 16 | rx_buf[10] << 8
889 | rx_buf[11]);
890 psoc_get_meas->timestamp_end = (rx_buf[12] << 24 |
891 rx_buf[13] << 16 | rx_buf[14] << 8
892 | rx_buf[15]);
893
894 for (i = 0; i < EPM_PSOC_BUFFERED_DATA_LENGTH; i++)
895 psoc_get_meas->buff_data[i] = rx_buf[16 + i];
896
897 return rc;
898}
899
900static int epm_psoc_timestamp(struct epm_adc_drv *epm_adc,
901 struct epm_psoc_system_time_stamp *psoc_timestamp)
902{
903 struct spi_message m;
904 struct spi_transfer t;
905 char tx_buf[10], rx_buf[10];
906 int rc = 0;
907
908 spi_setup(epm_adc->epm_spi_client);
909
910 memset(&t, 0, sizeof t);
911 memset(tx_buf, 0, sizeof tx_buf);
912 memset(rx_buf, 0, sizeof tx_buf);
913 t.tx_buf = tx_buf;
914 t.rx_buf = rx_buf;
915 spi_message_init(&m);
916 spi_message_add_tail(&t, &m);
917
918 if (psoc_timestamp->cmd == EPM_PSOC_SET_SYSTEM_TIMESTAMP_CMD) {
919 tx_buf[0] = psoc_timestamp->cmd;
920 tx_buf[1] = (psoc_timestamp->timestamp & 0xff000000) >> 24;
921 tx_buf[2] = (psoc_timestamp->timestamp & 0xff0000) >> 16;
922 tx_buf[3] = (psoc_timestamp->timestamp & 0xff00) >> 8;
923 tx_buf[4] = (psoc_timestamp->timestamp & 0xff);
924 } else if (psoc_timestamp->cmd == EPM_PSOC_GET_SYSTEM_TIMESTAMP_CMD) {
925 tx_buf[0] = psoc_timestamp->cmd;
926 }
927
928 t.len = sizeof(tx_buf);
929 t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD;
930
931 rc = spi_sync(epm_adc->epm_spi_client, &m);
932 if (rc)
933 return rc;
934
935 rc = spi_sync(epm_adc->epm_spi_client, &m);
936 if (rc)
937 return rc;
938
939 psoc_timestamp->cmd = rx_buf[0];
940 psoc_timestamp->timestamp = rx_buf[1] << 24 | rx_buf[2] << 16 |
941 rx_buf[3] << 8 | rx_buf[4];
942
943 return rc;
944}
945
946static int epm_psoc_get_avg_buffered_switch_data(struct epm_adc_drv *epm_adc,
947 struct epm_psoc_get_avg_buffered_switch_data *psoc_get_meas)
948{
949 struct spi_message m;
950 struct spi_transfer t;
951 char tx_buf[64], rx_buf[64];
952 int rc = 0, i;
953
954 spi_setup(epm_adc->epm_spi_client);
955
956 memset(&t, 0, sizeof t);
957 memset(tx_buf, 0, sizeof tx_buf);
958 memset(rx_buf, 0, sizeof tx_buf);
959 t.tx_buf = tx_buf;
960 t.rx_buf = rx_buf;
961 spi_message_init(&m);
962 spi_message_add_tail(&t, &m);
963
964 tx_buf[0] = psoc_get_meas->cmd;
965
966 t.len = sizeof(tx_buf);
967 t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD;
968
969 rc = spi_sync(epm_adc->epm_spi_client, &m);
970 if (rc)
971 return rc;
972
973 rc = spi_sync(epm_adc->epm_spi_client, &m);
974 if (rc)
975 return rc;
976
977 psoc_get_meas->cmd = rx_buf[0];
978 psoc_get_meas->status = rx_buf[1];
979 psoc_get_meas->timestamp_start = (rx_buf[2] << 24 |
980 rx_buf[3] << 16 | rx_buf[4] << 8
981 | rx_buf[5]);
982 psoc_get_meas->channel_mask = (rx_buf[6] << 24 |
983 rx_buf[7] << 16 | rx_buf[8] << 8
984 | rx_buf[9]);
985
986 for (i = 0; i < EPM_PSOC_BUFFERED_DATA_LENGTH2; i++)
987 psoc_get_meas->avg_data[i] = rx_buf[10 + i];
988
989 return rc;
990}
991
992static int epm_psoc_set_vadc(struct epm_adc_drv *epm_adc,
993 struct epm_psoc_set_vadc *psoc_set_vadc)
994{
995 struct spi_message m;
996 struct spi_transfer t;
997 char tx_buf[10], rx_buf[10];
998 int rc = 0;
999
1000 spi_setup(epm_adc->epm_spi_client);
1001
1002 memset(&t, 0, sizeof t);
1003 memset(tx_buf, 0, sizeof tx_buf);
1004 memset(rx_buf, 0, sizeof tx_buf);
1005 t.tx_buf = tx_buf;
1006 t.rx_buf = rx_buf;
1007 spi_message_init(&m);
1008 spi_message_add_tail(&t, &m);
1009
1010 tx_buf[0] = psoc_set_vadc->cmd;
1011 tx_buf[1] = psoc_set_vadc->vadc_dev;
1012 tx_buf[2] = (psoc_set_vadc->vadc_voltage & 0xff000000) >> 24;
1013 tx_buf[3] = (psoc_set_vadc->vadc_voltage & 0xff0000) >> 16;
1014 tx_buf[4] = (psoc_set_vadc->vadc_voltage & 0xff00) >> 8;
1015 tx_buf[5] = psoc_set_vadc->vadc_voltage & 0xff;
1016
1017 t.len = sizeof(tx_buf);
1018 t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD;
1019
1020 rc = spi_sync(epm_adc->epm_spi_client, &m);
1021 if (rc)
1022 return rc;
1023
1024 rc = spi_sync(epm_adc->epm_spi_client, &m);
1025 if (rc)
1026 return rc;
1027
1028 psoc_set_vadc->cmd = rx_buf[0];
1029 psoc_set_vadc->vadc_dev = rx_buf[1];
1030 psoc_set_vadc->vadc_voltage = (rx_buf[2] << 24) | (rx_buf[3] << 16) |
1031 (rx_buf[4] << 8) | (rx_buf[5]);
1032
1033 return rc;
1034}
1035
1036static int epm_psoc_set_channel_switch(struct epm_adc_drv *epm_adc,
1037 struct epm_psoc_set_channel_switch *psoc_channel_switch)
1038{
1039 struct spi_message m;
1040 struct spi_transfer t;
1041 char tx_buf[10], rx_buf[10];
1042 int rc = 0;
1043
1044 spi_setup(epm_adc->epm_spi_client);
1045
1046 memset(&t, 0, sizeof t);
1047 memset(tx_buf, 0, sizeof tx_buf);
1048 memset(rx_buf, 0, sizeof tx_buf);
1049 t.tx_buf = tx_buf;
1050 t.rx_buf = rx_buf;
1051 spi_message_init(&m);
1052 spi_message_add_tail(&t, &m);
1053
1054 tx_buf[0] = psoc_channel_switch->cmd;
1055 tx_buf[1] = psoc_channel_switch->dev;
1056 tx_buf[2] = (psoc_channel_switch->delay & 0xff000000) >> 24;
1057 tx_buf[3] = (psoc_channel_switch->delay & 0xff0000) >> 16;
1058 tx_buf[4] = (psoc_channel_switch->delay & 0xff00) >> 8;
1059 tx_buf[5] = psoc_channel_switch->delay & 0xff;
1060
1061 t.len = sizeof(tx_buf);
1062 t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD;
1063
1064 rc = spi_sync(epm_adc->epm_spi_client, &m);
1065 if (rc)
1066 return rc;
1067
1068 rc = spi_sync(epm_adc->epm_spi_client, &m);
1069 if (rc)
1070 return rc;
1071
1072 psoc_channel_switch->cmd = rx_buf[0];
1073 psoc_channel_switch->dev = rx_buf[1];
1074 psoc_channel_switch->delay = rx_buf[2] << 24 |
1075 rx_buf[3] << 16 |
1076 rx_buf[4] << 8 | rx_buf[5];
1077
1078 return rc;
1079}
1080
1081static int epm_psoc_clear_buffer(struct epm_adc_drv *epm_adc)
1082{
1083 struct spi_message m;
1084 struct spi_transfer t;
1085 char tx_buf[3], rx_buf[3];
1086 int rc = 0;
1087
1088 spi_setup(epm_adc->epm_spi_client);
1089
1090 memset(&t, 0, sizeof t);
1091 memset(tx_buf, 0, sizeof tx_buf);
1092 memset(rx_buf, 0, sizeof tx_buf);
1093 t.tx_buf = tx_buf;
1094 t.rx_buf = rx_buf;
1095 spi_message_init(&m);
1096 spi_message_add_tail(&t, &m);
1097
1098 tx_buf[0] = EPM_PSOC_CLEAR_BUFFER_CMD;
1099
1100 t.len = sizeof(tx_buf);
1101 t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD;
1102
1103 rc = spi_sync(epm_adc->epm_spi_client, &m);
1104 if (rc)
1105 return rc;
1106
1107 rc = spi_sync(epm_adc->epm_spi_client, &m);
1108 if (rc)
1109 return rc;
1110
1111 rc = rx_buf[2];
1112
1113 return rc;
1114}
1115
Siddartha Mohanadosseb90c922012-03-27 22:58:06 -07001116static long epm_adc_ioctl(struct file *file, unsigned int cmd,
1117 unsigned long arg)
1118{
1119 struct epm_adc_drv *epm_adc = epm_adc_drv;
1120
1121 switch (cmd) {
1122 case EPM_ADC_REQUEST:
1123 {
1124 struct epm_chan_request conv;
1125 int rc;
1126
1127 if (copy_from_user(&conv, (void __user *)arg,
1128 sizeof(struct epm_chan_request)))
1129 return -EFAULT;
1130
1131 rc = epm_adc_blocking_conversion(epm_adc, &conv);
1132 if (rc) {
1133 pr_err("Failed EPM conversion:%d\n", rc);
1134 return rc;
1135 }
1136
1137 if (copy_to_user((void __user *)arg, &conv,
1138 sizeof(struct epm_chan_request)))
1139 return -EFAULT;
1140 break;
1141 }
1142 case EPM_ADC_INIT:
1143 {
1144 uint32_t result;
1145 if (!epm_adc_expander_register) {
1146 result = epm_adc_i2c_expander_register();
1147 if (result) {
1148 pr_err("Failed i2c register:%d\n",
1149 result);
1150 return result;
1151 }
1152 epm_adc_expander_register = true;
1153 }
1154
Siddartha Mohanadossb0b8a6e2012-09-04 08:23:43 -07001155 result = epm_adc_hw_init(epm_adc);
Siddartha Mohanadosseb90c922012-03-27 22:58:06 -07001156
1157 if (copy_to_user((void __user *)arg, &result,
1158 sizeof(uint32_t)))
1159 return -EFAULT;
1160 break;
1161 }
1162 case EPM_ADC_DEINIT:
1163 {
1164 uint32_t result;
Siddartha Mohanadossb0b8a6e2012-09-04 08:23:43 -07001165 result = epm_adc_hw_deinit(epm_adc);
Siddartha Mohanadosseb90c922012-03-27 22:58:06 -07001166
1167 if (copy_to_user((void __user *)arg, &result,
1168 sizeof(uint32_t)))
1169 return -EFAULT;
1170 break;
1171 }
Siddartha Mohanadossb0b8a6e2012-09-04 08:23:43 -07001172 case EPM_PSOC_ADC_INIT:
1173 {
1174 struct epm_psoc_init_resp psoc_init;
1175 int rc;
1176
1177 if (copy_from_user(&psoc_init, (void __user *)arg,
1178 sizeof(struct epm_psoc_init_resp)))
1179 return -EFAULT;
1180
1181 psoc_init.cmd = EPM_PSOC_INIT_CMD;
1182 rc = epm_psoc_init(epm_adc, &psoc_init);
1183 if (rc) {
1184 pr_err("PSOC initialization failed\n");
1185 return -EINVAL;
1186 }
1187
1188 if (copy_to_user((void __user *)arg, &psoc_init,
1189 sizeof(struct epm_psoc_init_resp)))
1190 return -EFAULT;
1191 break;
1192 }
1193 case EPM_PSOC_ADC_CHANNEL_ENABLE:
1194 case EPM_PSOC_ADC_CHANNEL_DISABLE:
1195 {
1196 struct epm_psoc_channel_configure psoc_chan_configure;
1197 int rc;
1198
1199 if (copy_from_user(&psoc_chan_configure,
1200 (void __user *)arg,
1201 sizeof(struct epm_psoc_channel_configure)))
1202 return -EFAULT;
1203
1204 psoc_chan_configure.cmd =
1205 EPM_PSOC_CHANNEL_ENABLE_DISABLE_CMD;
1206 rc = epm_psoc_channel_configure(epm_adc,
1207 &psoc_chan_configure);
1208 if (rc) {
1209 pr_err("PSOC channel configure failed\n");
1210 return -EINVAL;
1211 }
1212
1213 if (copy_to_user((void __user *)arg,
1214 &psoc_chan_configure,
1215 sizeof(struct epm_psoc_channel_configure)))
1216 return -EFAULT;
1217 break;
1218 }
1219 case EPM_PSOC_ADC_SET_AVERAGING:
1220 {
1221 struct epm_psoc_set_avg psoc_set_avg;
1222 int rc;
1223
1224 if (copy_from_user(&psoc_set_avg, (void __user *)arg,
1225 sizeof(struct epm_psoc_set_avg)))
1226 return -EFAULT;
1227
1228 psoc_set_avg.cmd = EPM_PSOC_SET_AVERAGING_CMD;
1229 rc = epm_psoc_set_averaging(epm_adc, &psoc_set_avg);
1230 if (rc) {
1231 pr_err("PSOC averaging failed\n");
1232 return -EINVAL;
1233 }
1234
1235 if (copy_to_user((void __user *)arg, &psoc_set_avg,
1236 sizeof(struct epm_psoc_set_avg)))
1237 return -EFAULT;
1238 break;
1239 }
1240 case EPM_PSOC_ADC_GET_LAST_MEASUREMENT:
1241 {
1242 struct epm_psoc_get_data psoc_get_data;
1243 int rc;
1244
1245 if (copy_from_user(&psoc_get_data,
1246 (void __user *)arg,
1247 sizeof(struct epm_psoc_get_data)))
1248 return -EFAULT;
1249
1250 psoc_get_data.cmd = EPM_PSOC_GET_LAST_MEASUREMENT_CMD;
1251 rc = epm_psoc_get_data(epm_adc, &psoc_get_data);
1252 if (rc) {
1253 pr_err("PSOC last measured data failed\n");
1254 return -EINVAL;
1255 }
1256
1257 if (copy_to_user((void __user *)arg, &psoc_get_data,
1258 sizeof(struct epm_psoc_get_data)))
1259 return -EFAULT;
1260 break;
1261 }
1262 case EPM_PSOC_ADC_GET_BUFFERED_DATA:
1263 {
1264 struct epm_psoc_get_buffered_data psoc_get_data;
1265 int rc;
1266
1267 if (copy_from_user(&psoc_get_data,
1268 (void __user *)arg,
1269 sizeof(struct epm_psoc_get_buffered_data)))
1270 return -EFAULT;
1271
1272 psoc_get_data.cmd = EPM_PSOC_GET_BUFFERED_DATA_CMD;
1273 rc = epm_psoc_get_buffered_data(epm_adc,
1274 &psoc_get_data);
1275 if (rc) {
1276 pr_err("PSOC buffered measurement failed\n");
1277 return -EINVAL;
1278 }
1279
1280 if (copy_to_user((void __user *)arg, &psoc_get_data,
1281 sizeof(struct epm_psoc_get_buffered_data)))
1282 return -EFAULT;
1283 break;
1284 }
1285 case EPM_PSOC_ADC_GET_SYSTEM_TIMESTAMP:
1286 case EPM_PSOC_ADC_SET_SYSTEM_TIMESTAMP:
1287 {
1288 struct epm_psoc_system_time_stamp psoc_timestamp;
1289 int rc;
1290
1291 if (copy_from_user(&psoc_timestamp,
1292 (void __user *)arg,
1293 sizeof(struct epm_psoc_system_time_stamp)))
1294 return -EFAULT;
1295
1296 rc = epm_psoc_timestamp(epm_adc, &psoc_timestamp);
1297 if (rc) {
1298 pr_err("PSOC buffered measurement failed\n");
1299 return -EINVAL;
1300 }
1301
1302 if (copy_to_user((void __user *)arg, &psoc_timestamp,
1303 sizeof(struct epm_psoc_system_time_stamp)))
1304 return -EFAULT;
1305 break;
1306 }
1307 case EPM_PSOC_ADC_GET_AVERAGE_DATA:
1308 {
1309 struct epm_psoc_get_avg_buffered_switch_data
1310 psoc_get_data;
1311 int rc;
1312
1313 if (copy_from_user(&psoc_get_data,
1314 (void __user *)arg,
1315 sizeof(struct
1316 epm_psoc_get_avg_buffered_switch_data)))
1317 return -EFAULT;
1318
1319 psoc_get_data.cmd = EPM_PSOC_GET_AVERAGED_DATA_CMD;
1320 rc = epm_psoc_get_avg_buffered_switch_data(epm_adc,
1321 &psoc_get_data);
1322 if (rc) {
1323 pr_err("Get averaged buffered data failed\n");
1324 return -EINVAL;
1325 }
1326
1327 if (copy_to_user((void __user *)arg, &psoc_get_data,
1328 sizeof(struct
1329 epm_psoc_get_avg_buffered_switch_data)))
1330 return -EFAULT;
1331 break;
1332 }
1333 case EPM_PSOC_SET_CHANNEL_SWITCH:
1334 {
1335 struct epm_psoc_set_channel_switch psoc_channel_switch;
1336 int rc;
1337
1338 if (copy_from_user(&psoc_channel_switch,
1339 (void __user *)arg,
1340 sizeof(struct epm_psoc_set_channel_switch)))
1341 return -EFAULT;
1342
1343 rc = epm_psoc_set_channel_switch(epm_adc,
1344 &psoc_channel_switch);
1345 if (rc) {
1346 pr_err("PSOC channel switch failed\n");
1347 return -EINVAL;
1348 }
1349
1350 if (copy_to_user((void __user *)arg,
1351 &psoc_channel_switch,
1352 sizeof(struct epm_psoc_set_channel_switch)))
1353 return -EFAULT;
1354 break;
1355 }
1356 case EPM_PSOC_CLEAR_BUFFER:
1357 {
1358 int rc;
1359 rc = epm_psoc_clear_buffer(epm_adc);
1360 if (rc) {
1361 pr_err("PSOC clear buffer failed\n");
1362 return -EINVAL;
1363 }
1364
1365 if (copy_to_user((void __user *)arg, &rc,
1366 sizeof(uint32_t)))
1367 return -EFAULT;
1368 break;
1369 }
1370 case EPM_PSOC_ADC_SET_VADC_REFERENCE:
1371 {
1372 struct epm_psoc_set_vadc psoc_set_vadc;
1373 int rc;
1374
1375 if (copy_from_user(&psoc_set_vadc,
1376 (void __user *)arg,
1377 sizeof(struct epm_psoc_set_vadc)))
1378 return -EFAULT;
1379
1380 rc = epm_psoc_set_vadc(epm_adc, &psoc_set_vadc);
1381 if (rc) {
1382 pr_err("PSOC set VADC failed\n");
1383 return -EINVAL;
1384 }
1385
1386 if (copy_to_user((void __user *)arg, &psoc_set_vadc,
1387 sizeof(struct epm_psoc_set_vadc)))
1388 return -EFAULT;
1389 break;
1390 }
Siddartha Mohanadosseb90c922012-03-27 22:58:06 -07001391 default:
1392 return -EINVAL;
1393 }
1394
1395 return 0;
1396}
1397
1398const struct file_operations epm_adc_fops = {
1399 .unlocked_ioctl = epm_adc_ioctl,
1400};
1401
Siddartha Mohanadossb0b8a6e2012-09-04 08:23:43 -07001402static ssize_t epm_adc_psoc_show_in(struct device *dev,
1403 struct device_attribute *devattr, char *buf)
1404{
1405 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1406 struct epm_adc_drv *epm_adc = epm_adc_drv;
1407 struct epm_psoc_init_resp init_resp;
1408 struct epm_psoc_channel_configure psoc_chan_configure;
1409 struct epm_psoc_get_data psoc_get_meas;
1410 int16_t *adc_code = 0;
1411 int rc = 0;
1412
1413 rc = epm_adc_psoc_gpio_init(true);
1414 if (rc) {
1415 pr_err("GPIO init failed\n");
1416 return 0;
1417 }
1418 usleep_range(EPM_GLOBAL_ENABLE_MIN_DELAY,
1419 EPM_GLOBAL_ENABLE_MAX_DELAY);
1420
1421 init_resp.cmd = EPM_PSOC_INIT_CMD;
1422 rc = epm_psoc_init(epm_adc, &init_resp);
1423 if (rc) {
1424 pr_info("PSOC init failed %d\n", rc);
1425 return 0;
1426 }
1427
1428 psoc_chan_configure.channel_num = (1 << attr->index);
1429 psoc_chan_configure.cmd = EPM_PSOC_CHANNEL_ENABLE_DISABLE_CMD;
1430 rc = epm_psoc_channel_configure(epm_adc, &psoc_chan_configure);
1431 if (rc) {
1432 pr_info("PSOC channel configure failed\n");
1433 return 0;
1434 }
1435
1436 usleep_range(EPM_GLOBAL_ENABLE_MIN_DELAY,
1437 EPM_GLOBAL_ENABLE_MAX_DELAY);
1438
1439 psoc_get_meas.cmd = EPM_PSOC_GET_LAST_MEASUREMENT_CMD;
1440 psoc_get_meas.dev_num = 0;
1441 psoc_get_meas.chan_num = attr->index;
1442 rc = epm_psoc_get_data(epm_adc, &psoc_get_meas);
1443 if (rc) {
1444 pr_info("PSOC get data failed\n");
1445 return 0;
1446 }
1447
1448 *adc_code = psoc_get_meas.reading_value;
1449
1450 rc = epm_psoc_scale_result(adc_code,
1451 psoc_chan_configure.channel_num);
1452 if (rc) {
1453 pr_info("Scale result failed\n");
1454 return 0;
1455 }
1456
1457 psoc_get_meas.reading_value = *adc_code;
1458
1459 rc = epm_adc_psoc_gpio_init(false);
1460 if (rc) {
1461 pr_err("GPIO de-init failed\n");
1462 return 0;
1463 }
1464
1465 return snprintf(buf, 16, "Result: %d\n", psoc_get_meas.reading_value);
1466}
1467
1468static struct sensor_device_attribute epm_adc_psoc_in_attrs[] = {
1469 SENSOR_ATTR(ads0_chan0, S_IRUGO, epm_adc_psoc_show_in, NULL, 0),
1470 SENSOR_ATTR(ads0_chan1, S_IRUGO, epm_adc_psoc_show_in, NULL, 1),
1471 SENSOR_ATTR(ads0_chan2, S_IRUGO, epm_adc_psoc_show_in, NULL, 2),
1472 SENSOR_ATTR(ads0_chan3, S_IRUGO, epm_adc_psoc_show_in, NULL, 3),
1473 SENSOR_ATTR(ads0_chan4, S_IRUGO, epm_adc_psoc_show_in, NULL, 4),
1474 SENSOR_ATTR(ads0_chan5, S_IRUGO, epm_adc_psoc_show_in, NULL, 5),
1475 SENSOR_ATTR(ads0_chan6, S_IRUGO, epm_adc_psoc_show_in, NULL, 6),
1476 SENSOR_ATTR(ads0_chan7, S_IRUGO, epm_adc_psoc_show_in, NULL, 7),
1477 SENSOR_ATTR(ads0_chan8, S_IRUGO, epm_adc_psoc_show_in, NULL, 8),
1478 SENSOR_ATTR(ads0_chan9, S_IRUGO, epm_adc_psoc_show_in, NULL, 9),
1479 SENSOR_ATTR(ads0_chan10, S_IRUGO, epm_adc_psoc_show_in, NULL, 10),
1480 SENSOR_ATTR(ads0_chan11, S_IRUGO, epm_adc_psoc_show_in, NULL, 11),
1481 SENSOR_ATTR(ads0_chan12, S_IRUGO, epm_adc_psoc_show_in, NULL, 12),
1482 SENSOR_ATTR(ads0_chan13, S_IRUGO, epm_adc_psoc_show_in, NULL, 13),
1483 SENSOR_ATTR(ads0_chan14, S_IRUGO, epm_adc_psoc_show_in, NULL, 14),
1484 SENSOR_ATTR(ads0_chan15, S_IRUGO, epm_adc_psoc_show_in, NULL, 15),
1485 SENSOR_ATTR(ads1_chan0, S_IRUGO, epm_adc_psoc_show_in, NULL, 16),
1486 SENSOR_ATTR(ads1_chan1, S_IRUGO, epm_adc_psoc_show_in, NULL, 17),
1487 SENSOR_ATTR(ads1_chan2, S_IRUGO, epm_adc_psoc_show_in, NULL, 18),
1488 SENSOR_ATTR(ads1_chan3, S_IRUGO, epm_adc_psoc_show_in, NULL, 19),
1489 SENSOR_ATTR(ads1_chan4, S_IRUGO, epm_adc_psoc_show_in, NULL, 20),
1490 SENSOR_ATTR(ads1_chan5, S_IRUGO, epm_adc_psoc_show_in, NULL, 21),
1491 SENSOR_ATTR(ads1_chan6, S_IRUGO, epm_adc_psoc_show_in, NULL, 22),
1492 SENSOR_ATTR(ads1_chan7, S_IRUGO, epm_adc_psoc_show_in, NULL, 23),
1493 SENSOR_ATTR(ads1_chan8, S_IRUGO, epm_adc_psoc_show_in, NULL, 24),
1494 SENSOR_ATTR(ads1_chan9, S_IRUGO, epm_adc_psoc_show_in, NULL, 25),
1495 SENSOR_ATTR(ads1_chan10, S_IRUGO, epm_adc_psoc_show_in, NULL, 26),
1496 SENSOR_ATTR(ads1_chan11, S_IRUGO, epm_adc_psoc_show_in, NULL, 27),
1497 SENSOR_ATTR(ads1_chan12, S_IRUGO, epm_adc_psoc_show_in, NULL, 28),
1498 SENSOR_ATTR(ads1_chan13, S_IRUGO, epm_adc_psoc_show_in, NULL, 29),
1499 SENSOR_ATTR(ads1_chan14, S_IRUGO, epm_adc_psoc_show_in, NULL, 30),
1500 SENSOR_ATTR(ads1_chan15, S_IRUGO, epm_adc_psoc_show_in, NULL, 31),
1501};
1502
1503static int __devinit epm_adc_psoc_init_hwmon(struct spi_device *spi,
1504 struct epm_adc_drv *epm_adc)
1505{
1506 int i, rc, num_chans = 15;
1507
1508 for (i = 0; i < num_chans; i++) {
1509 rc = device_create_file(&spi->dev,
1510 &epm_adc_psoc_in_attrs[i].dev_attr);
1511 if (rc) {
1512 dev_err(&spi->dev, "device_create_file failed\n");
1513 return rc;
1514 }
1515 }
1516
1517 return 0;
1518}
1519
1520static int get_device_tree_data(struct spi_device *spi)
1521{
1522 const struct device_node *node = spi->dev.of_node;
1523 struct epm_adc_drv *epm_adc;
1524 int32_t *epm_ch_gain, *epm_ch_rsense;
1525 u32 rc = 0, epm_num_channels, i;
1526
1527 if (!node)
1528 return -EINVAL;
1529
1530 rc = of_property_read_u32(node,
1531 "qcom,channels", &epm_num_channels);
1532 if (rc) {
1533 dev_err(&spi->dev, "missing channel numbers\n");
1534 return -ENODEV;
1535 }
1536
1537 epm_ch_gain = devm_kzalloc(&spi->dev,
1538 epm_num_channels, GFP_KERNEL);
1539 if (!epm_ch_gain) {
1540 dev_err(&spi->dev, "cannot allocate gain\n");
1541 return -ENOMEM;
1542 }
1543
1544 epm_ch_rsense = devm_kzalloc(&spi->dev,
1545 epm_num_channels, GFP_KERNEL);
1546 if (!epm_ch_rsense) {
1547 dev_err(&spi->dev, "cannot allocate rsense\n");
1548 return -ENOMEM;
1549 }
1550
1551 rc = of_property_read_u32_array(node,
1552 "qcom,gain", epm_ch_gain, epm_num_channels);
1553 if (rc) {
1554 dev_err(&spi->dev, "invalid gain property:%d\n", rc);
1555 return rc;
1556 }
1557
1558 rc = of_property_read_u32_array(node,
1559 "qcom,rsense", epm_ch_rsense, epm_num_channels);
1560 if (rc) {
1561 dev_err(&spi->dev, "invalid rsense property:%d\n", rc);
1562 return rc;
1563 }
1564
1565 epm_adc = devm_kzalloc(&spi->dev,
1566 sizeof(struct epm_adc_drv) +
1567 (epm_num_channels *
1568 sizeof(struct epm_chan_properties)),
1569 GFP_KERNEL);
1570 if (!epm_adc) {
1571 dev_err(&spi->dev, "Unable to allocate memory\n");
1572 return -ENOMEM;
1573 }
1574
1575 for (i = 0; i < epm_num_channels; i++) {
1576 epm_adc->epm_psoc_ch_prop[i].resistorvalue =
1577 epm_ch_rsense[i];
1578 epm_adc->epm_psoc_ch_prop[i].gain =
1579 epm_ch_gain[i];
1580 }
1581
1582 epm_adc_drv = epm_adc;
1583
1584 return 0;
1585}
1586
1587static int __devinit epm_adc_psoc_spi_probe(struct spi_device *spi)
1588{
1589 struct epm_adc_drv *epm_adc;
1590 struct device_node *node = spi->dev.of_node;
1591 int rc = 0;
1592
Siddartha Mohanadossb0b8a6e2012-09-04 08:23:43 -07001593 if (node)
1594 rc = get_device_tree_data(spi);
Siddartha Mohanadosscd7672f2012-09-18 16:57:50 -07001595 else {
1596 epm_adc = epm_adc_drv;
1597 epm_adc_drv->epm_spi_client = spi;
1598 epm_adc_drv->epm_spi_client->bits_per_word =
1599 EPM_ADC_ADS_SPI_BITS_PER_WORD;
1600 return rc;
1601 }
Siddartha Mohanadossb0b8a6e2012-09-04 08:23:43 -07001602
1603 epm_adc = epm_adc_drv;
1604 epm_adc->misc.name = EPM_ADC_DRIVER_NAME;
1605 epm_adc->misc.minor = MISC_DYNAMIC_MINOR;
1606 epm_adc_drv->epm_spi_client = spi;
1607 epm_adc_drv->epm_spi_client->bits_per_word =
1608 EPM_ADC_ADS_SPI_BITS_PER_WORD;
1609 rc = epm_adc_psoc_init_hwmon(spi, epm_adc);
1610 if (rc) {
1611 dev_err(&spi->dev, "msm_adc_dev_init failed\n");
1612 return rc;
1613 }
1614
1615 epm_adc->hwmon = hwmon_device_register(&spi->dev);
1616 if (IS_ERR(epm_adc->hwmon)) {
1617 dev_err(&spi->dev, "hwmon_device_register failed\n");
1618 return rc;
1619 }
1620
1621 mutex_init(&epm_adc->conv_lock);
1622
1623 return rc;
1624}
1625
1626static int __devexit epm_adc_psoc_spi_remove(struct spi_device *spi)
1627{
1628 epm_adc_drv->epm_spi_client = NULL;
1629 return 0;
1630}
1631
1632static const struct of_device_id epm_adc_psoc_match_table[] = {
1633 { .compatible = "qcom,epm-adc",
1634 },
1635 {}
1636};
1637
1638static struct spi_driver epm_spi_driver = {
1639 .probe = epm_adc_psoc_spi_probe,
1640 .remove = __devexit_p(epm_adc_psoc_spi_remove),
1641 .driver = {
1642 .name = EPM_ADC_DRIVER_NAME,
1643 .of_match_table = epm_adc_psoc_match_table,
1644 },
1645};
1646
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -07001647static ssize_t epm_adc_show_in(struct device *dev,
1648 struct device_attribute *devattr, char *buf)
1649{
1650 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1651 struct epm_adc_drv *epm_adc = dev_get_drvdata(dev);
1652 struct epm_adc_platform_data *pdata = epm_adc->pdev->dev.platform_data;
1653 struct epm_chan_request conv;
1654 int rc = 0;
1655
1656 conv.device_idx = attr->index / pdata->chan_per_adc;
1657 conv.channel_idx = attr->index % pdata->chan_per_adc;
1658 conv.physical = 0;
1659 pr_debug("%s: device_idx=%d channel_idx=%d", __func__, conv.device_idx,
1660 conv.channel_idx);
Siddartha Mohanadosseb90c922012-03-27 22:58:06 -07001661 if (!epm_adc_expander_register) {
1662 rc = epm_adc_i2c_expander_register();
1663 if (rc) {
1664 pr_err("I2C expander register failed:%d\n", rc);
1665 return rc;
1666 }
1667 epm_adc_expander_register = true;
1668 }
1669
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -07001670 rc = epm_adc_hw_init(epm_adc);
1671 if (rc) {
1672 pr_err("%s: epm_adc_hw_init() failed, rc = %d",
1673 __func__, rc);
1674 return 0;
1675 }
1676
1677 rc = epm_adc_blocking_conversion(epm_adc, &conv);
1678 if (rc) {
1679 pr_err("%s: epm_adc_blocking_conversion() failed, rc = %d\n",
1680 __func__, rc);
1681 return 0;
1682 }
1683 rc = epm_adc_hw_deinit(epm_adc);
1684 if (rc) {
1685 pr_err("%s: epm_adc_hw_deinit() failed, rc = %d",
1686 __func__, rc);
1687 return 0;
1688 }
1689
1690 return snprintf(buf, 16, "Result: %d\n", conv.physical);
1691}
1692
Stephen Boydaeaf2ad2012-07-03 14:33:25 -07001693static struct sensor_device_attribute epm_adc_in_attrs[] = {
1694 SENSOR_ATTR(ads0_chan0, S_IRUGO, epm_adc_show_in, NULL, 0),
1695 SENSOR_ATTR(ads0_chan1, S_IRUGO, epm_adc_show_in, NULL, 1),
1696 SENSOR_ATTR(ads0_chan2, S_IRUGO, epm_adc_show_in, NULL, 2),
1697 SENSOR_ATTR(ads0_chan3, S_IRUGO, epm_adc_show_in, NULL, 3),
1698 SENSOR_ATTR(ads0_chan4, S_IRUGO, epm_adc_show_in, NULL, 4),
1699 SENSOR_ATTR(ads0_chan5, S_IRUGO, epm_adc_show_in, NULL, 5),
1700 SENSOR_ATTR(ads0_chan6, S_IRUGO, epm_adc_show_in, NULL, 6),
1701 SENSOR_ATTR(ads0_chan7, S_IRUGO, epm_adc_show_in, NULL, 7),
1702 SENSOR_ATTR(ads0_chan8, S_IRUGO, epm_adc_show_in, NULL, 8),
1703 SENSOR_ATTR(ads0_chan9, S_IRUGO, epm_adc_show_in, NULL, 9),
1704 SENSOR_ATTR(ads0_chan10, S_IRUGO, epm_adc_show_in, NULL, 10),
1705 SENSOR_ATTR(ads0_chan11, S_IRUGO, epm_adc_show_in, NULL, 11),
1706 SENSOR_ATTR(ads0_chan12, S_IRUGO, epm_adc_show_in, NULL, 12),
1707 SENSOR_ATTR(ads0_chan13, S_IRUGO, epm_adc_show_in, NULL, 13),
1708 SENSOR_ATTR(ads0_chan14, S_IRUGO, epm_adc_show_in, NULL, 14),
1709 SENSOR_ATTR(ads0_chan15, S_IRUGO, epm_adc_show_in, NULL, 15),
1710 SENSOR_ATTR(ads1_chan0, S_IRUGO, epm_adc_show_in, NULL, 16),
1711 SENSOR_ATTR(ads1_chan1, S_IRUGO, epm_adc_show_in, NULL, 17),
1712 SENSOR_ATTR(ads1_chan2, S_IRUGO, epm_adc_show_in, NULL, 18),
1713 SENSOR_ATTR(ads1_chan3, S_IRUGO, epm_adc_show_in, NULL, 19),
1714 SENSOR_ATTR(ads1_chan4, S_IRUGO, epm_adc_show_in, NULL, 20),
1715 SENSOR_ATTR(ads1_chan5, S_IRUGO, epm_adc_show_in, NULL, 21),
1716 SENSOR_ATTR(ads1_chan6, S_IRUGO, epm_adc_show_in, NULL, 22),
1717 SENSOR_ATTR(ads1_chan7, S_IRUGO, epm_adc_show_in, NULL, 23),
1718 SENSOR_ATTR(ads1_chan8, S_IRUGO, epm_adc_show_in, NULL, 24),
1719 SENSOR_ATTR(ads1_chan9, S_IRUGO, epm_adc_show_in, NULL, 25),
1720 SENSOR_ATTR(ads1_chan10, S_IRUGO, epm_adc_show_in, NULL, 26),
1721 SENSOR_ATTR(ads1_chan11, S_IRUGO, epm_adc_show_in, NULL, 27),
1722 SENSOR_ATTR(ads1_chan12, S_IRUGO, epm_adc_show_in, NULL, 28),
1723 SENSOR_ATTR(ads1_chan13, S_IRUGO, epm_adc_show_in, NULL, 29),
1724 SENSOR_ATTR(ads1_chan14, S_IRUGO, epm_adc_show_in, NULL, 30),
1725 SENSOR_ATTR(ads1_chan15, S_IRUGO, epm_adc_show_in, NULL, 31),
1726};
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -07001727
1728static int __devinit epm_adc_init_hwmon(struct platform_device *pdev,
1729 struct epm_adc_drv *epm_adc)
1730{
1731 struct epm_adc_platform_data *pdata = pdev->dev.platform_data;
Stephen Boydaeaf2ad2012-07-03 14:33:25 -07001732 int i, rc, num_chans = pdata->num_channels;
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -07001733
Stephen Boydaeaf2ad2012-07-03 14:33:25 -07001734 for (i = 0; i < num_chans; i++) {
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -07001735 rc = device_create_file(&pdev->dev,
Stephen Boydaeaf2ad2012-07-03 14:33:25 -07001736 &epm_adc_in_attrs[i].dev_attr);
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -07001737 if (rc) {
1738 dev_err(&pdev->dev, "device_create_file failed\n");
1739 return rc;
1740 }
1741 }
1742
Stephen Boydaeaf2ad2012-07-03 14:33:25 -07001743 return 0;
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -07001744}
1745
Stephen Boyd58701e82012-04-25 11:48:28 -07001746static int __devinit epm_adc_probe(struct platform_device *pdev)
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -07001747{
1748 struct epm_adc_drv *epm_adc;
1749 struct epm_adc_platform_data *pdata = pdev->dev.platform_data;
1750 int rc = 0;
1751
1752 if (!pdata) {
1753 dev_err(&pdev->dev, "no platform data?\n");
1754 return -EINVAL;
1755 }
1756
1757 epm_adc = kzalloc(sizeof(struct epm_adc_drv), GFP_KERNEL);
1758 if (!epm_adc) {
1759 dev_err(&pdev->dev, "Unable to allocate memory\n");
1760 return -ENOMEM;
1761 }
1762
1763 platform_set_drvdata(pdev, epm_adc);
1764 epm_adc_drv = epm_adc;
1765 epm_adc->pdev = pdev;
1766
Siddartha Mohanadosseb90c922012-03-27 22:58:06 -07001767 epm_adc->misc.name = EPM_ADC_DRIVER_NAME;
1768 epm_adc->misc.minor = MISC_DYNAMIC_MINOR;
1769 epm_adc->misc.fops = &epm_adc_fops;
1770
1771 if (misc_register(&epm_adc->misc)) {
1772 dev_err(&pdev->dev, "Unable to register misc device!\n");
1773 return -EFAULT;
1774 }
1775
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -07001776 rc = epm_adc_init_hwmon(pdev, epm_adc);
1777 if (rc) {
1778 dev_err(&pdev->dev, "msm_adc_dev_init failed\n");
Siddartha Mohanadosseb90c922012-03-27 22:58:06 -07001779 misc_deregister(&epm_adc->misc);
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -07001780 return rc;
1781 }
1782
1783 epm_adc->hwmon = hwmon_device_register(&pdev->dev);
1784 if (IS_ERR(epm_adc->hwmon)) {
1785 dev_err(&pdev->dev, "hwmon_device_register failed\n");
Siddartha Mohanadosseb90c922012-03-27 22:58:06 -07001786 misc_deregister(&epm_adc->misc);
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -07001787 rc = PTR_ERR(epm_adc->hwmon);
1788 return rc;
1789 }
1790
1791 mutex_init(&epm_adc->conv_lock);
1792 epm_i2c_info = &pdata->epm_i2c_board_info;
1793 epm_adc->bus_id = pdata->bus_id;
1794 epm_gpio_expander_base_addr = pdata->gpio_expander_base_addr;
Siddartha Mohanadosseb90c922012-03-27 22:58:06 -07001795 epm_adc_expander_register = false;
Siddartha Mohanadossb0b8a6e2012-09-04 08:23:43 -07001796
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -07001797 return rc;
1798}
1799
1800static int __devexit epm_adc_remove(struct platform_device *pdev)
1801{
1802 struct epm_adc_drv *epm_adc = platform_get_drvdata(pdev);
1803 struct epm_adc_platform_data *pdata = pdev->dev.platform_data;
1804 int num_chans = pdata->num_channels;
1805 int i = 0;
1806
Stephen Boydaeaf2ad2012-07-03 14:33:25 -07001807 for (i = 0; i < num_chans; i++)
1808 device_remove_file(&pdev->dev, &epm_adc_in_attrs[i].dev_attr);
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -07001809 hwmon_device_unregister(epm_adc->hwmon);
Siddartha Mohanadosseb90c922012-03-27 22:58:06 -07001810 misc_deregister(&epm_adc->misc);
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -07001811 epm_adc = NULL;
1812
1813 return 0;
1814}
1815
1816static struct platform_driver epm_adc_driver = {
1817 .probe = epm_adc_probe,
1818 .remove = __devexit_p(epm_adc_remove),
1819 .driver = {
1820 .name = EPM_ADC_DRIVER_NAME,
1821 .owner = THIS_MODULE,
1822 },
1823};
1824
1825static int __init epm_adc_init(void)
1826{
1827 int ret = 0;
1828
1829 ret = platform_driver_register(&epm_adc_driver);
1830 if (ret) {
1831 pr_err("%s: driver register failed, rc=%d\n", __func__, ret);
1832 return ret;
1833 }
1834
1835 ret = spi_register_driver(&epm_spi_driver);
1836 if (ret)
1837 pr_err("%s: spi register failed: rc=%d\n", __func__, ret);
1838
1839 return ret;
1840}
1841
1842static void __exit epm_adc_exit(void)
1843{
1844 spi_unregister_driver(&epm_spi_driver);
1845 platform_driver_unregister(&epm_adc_driver);
1846}
1847
1848module_init(epm_adc_init);
1849module_exit(epm_adc_exit);
1850
1851MODULE_DESCRIPTION("EPM ADC Driver");
1852MODULE_ALIAS("platform:epm_adc");
1853MODULE_LICENSE("GPL v2");