blob: 2f5ed9d953f86809486e2d6a0998132193f4ad0f [file] [log] [blame]
Siddartha Mohanadoss899173a2013-01-29 19:18:50 -08001/* Copyright (c) 2012-2013, 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
Siddartha Mohanadoss3efc4d82013-06-11 15:34:52 -070068#define GPIO_EPM_MARKER1 85
69#define GPIO_EPM_MARKER2 96
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -070070#define EPM_ADC_CONVERSION_TIME_MIN 50000
71#define EPM_ADC_CONVERSION_TIME_MAX 51000
Siddartha Mohanadossefddea42012-09-04 08:23:43 -070072/* PSoc Commands */
73#define EPM_PSOC_INIT_CMD 0x1
74#define EPM_PSOC_INIT_RESPONSE_CMD 0x2
75#define EPM_PSOC_CHANNEL_ENABLE_DISABLE_CMD 0x5
76#define EPM_PSOC_CHANNEL_ENABLE_DISABLE_RESPONSE_CMD 0x6
77#define EPM_PSOC_SET_AVERAGING_CMD 0x7
78#define EPM_PSOC_SET_AVERAGING_RESPONSE_CMD 0x8
79#define EPM_PSOC_GET_LAST_MEASUREMENT_CMD 0x9
80#define EPM_PSOC_GET_LAST_MEASUREMENT_RESPONSE_CMD 0xa
81#define EPM_PSOC_GET_BUFFERED_DATA_CMD 0xb
82#define EPM_PSOC_GET_BUFFERED_RESPONSE_CMD 0xc
83#define EPM_PSOC_GET_SYSTEM_TIMESTAMP_CMD 0x11
84#define EPM_PSOC_GET_SYSTEM_TIMESTAMP_RESPONSE_CMD 0x12
85#define EPM_PSOC_SET_SYSTEM_TIMESTAMP_CMD 0x13
86#define EPM_PSOC_SET_SYSTEM_TIMESTAMP_RESPONSE_CMD 0x14
87#define EPM_PSOC_SET_CHANNEL_TYPE_CMD 0x15
88#define EPM_PSOC_SET_CHANNEL_TYPE_RESPONSE_CMD 0x16
89#define EPM_PSOC_GET_AVERAGED_DATA_CMD 0x19
90#define EPM_PSOC_GET_AVERAGED_DATA_RESPONSE_CMD 0x1a
91#define EPM_PSOC_SET_CHANNEL_SWITCH_DELAY_CMD 0x1b
92#define EPM_PSOC_SET_CHANNEL_SWITCH_DELAY_RESPONSE_CMD 0x1c
93#define EPM_PSOC_CLEAR_BUFFER_CMD 0x1d
94#define EPM_PSOC_CLEAR_BUFFER_RESPONSE_CMD 0x1e
95#define EPM_PSOC_SET_VADC_REFERENCE_CMD 0x1f
96#define EPM_PSOC_SET_VADC_REFERENCE_RESPONSE_CMD 0x20
Siddartha Mohanadoss46aff912013-07-10 18:53:48 -070097#define EPM_PSOC_PAUSE_CONVERSION 0x35
98#define EPM_PSOC_PAUSE_CONVERSION_RSP_CMD 0x36
99#define EPM_PSOC_UNPAUSE_CONVERSION 0x37
100#define EPM_PSOC_UNPAUSE_CONVERSION_RSP_CMD 0x38
Siddartha Mohanadossd34827d2013-06-12 09:57:30 -0700101#define EPM_PSOC_GPIO_BUFFER_REQUEST_CMD 0x4f
102#define EPM_PSOC_GPIO_BUFFER_REQUEST_RESPONSE_CMD 0x50
103#define EPM_PSOC_GET_GPIO_BUFFER_CMD 0x51
104#define EPM_PSOC_GET_GPIO_BUFFER_RESPONSE_CMD 0x52
Siddartha Mohanadossefddea42012-09-04 08:23:43 -0700105
106#define EPM_PSOC_GLOBAL_ENABLE 81
107#define EPM_PSOC_VREF_VOLTAGE 2048
Siddartha Mohanadossae7acf82012-12-12 10:36:07 -0800108#define EPM_PSOC_MAX_ADC_CODE_15_BIT 32767
109#define EPM_PSOC_MAX_ADC_CODE_12_BIT 4096
Siddartha Mohanadossefddea42012-09-04 08:23:43 -0700110#define EPM_GLOBAL_ENABLE_MIN_DELAY 5000
111#define EPM_GLOBAL_ENABLE_MAX_DELAY 5100
112
Siddartha Mohanadoss6e77b772012-09-19 21:58:13 -0700113#define EPM_AVG_BUF_MASK1 0xfff00000
114#define EPM_AVG_BUF_MASK2 0xfff00
115#define EPM_AVG_BUF_MASK3 0xff
116#define EPM_AVG_BUF_MASK4 0xf0000000
117#define EPM_AVG_BUF_MASK5 0xfff0000
118#define EPM_AVG_BUF_MASK6 0xfff0
119#define EPM_AVG_BUF_MASK7 0xf
120#define EPM_AVG_BUF_MASK8 0xff000000
121#define EPM_AVG_BUF_MASK9 0xfff000
122#define EPM_AVG_BUF_MASK10 0xfff
123
Siddartha Mohanadossefddea42012-09-04 08:23:43 -0700124#define EPM_PSOC_BUFFERED_DATA_LENGTH 48
125#define EPM_PSOC_BUFFERED_DATA_LENGTH2 54
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700126
127struct epm_adc_drv {
128 struct platform_device *pdev;
129 struct device *hwmon;
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700130 struct spi_device *epm_spi_client;
131 struct mutex conv_lock;
132 uint32_t bus_id;
Siddartha Mohanadosseb90c922012-03-27 22:58:06 -0700133 struct miscdevice misc;
Siddartha Mohanadoss6e77b772012-09-19 21:58:13 -0700134 uint32_t channel_mask;
Siddartha Mohanadossefddea42012-09-04 08:23:43 -0700135 struct epm_chan_properties epm_psoc_ch_prop[0];
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700136};
137
138static struct epm_adc_drv *epm_adc_drv;
139static struct i2c_board_info *epm_i2c_info;
140static bool epm_adc_first_request;
141static int epm_gpio_expander_base_addr;
Siddartha Mohanadosseb90c922012-03-27 22:58:06 -0700142static bool epm_adc_expander_register;
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700143
144#define GPIO_EPM_EXPANDER_IO0 epm_gpio_expander_base_addr
145#define GPIO_PWR_MON_ENABLE (GPIO_EPM_EXPANDER_IO0 + 1)
146#define GPIO_ADC1_PWDN_N (GPIO_PWR_MON_ENABLE + 1)
147#define GPIO_PWR_MON_RESET_N (GPIO_ADC1_PWDN_N + 1)
148#define GPIO_EPM_SPI_ADC1_CS_N (GPIO_PWR_MON_RESET_N + 1)
149#define GPIO_PWR_MON_START (GPIO_EPM_SPI_ADC1_CS_N + 1)
150#define GPIO_ADC1_DRDY_N (GPIO_PWR_MON_START + 1)
151#define GPIO_ADC2_PWDN_N (GPIO_ADC1_DRDY_N + 1)
152#define GPIO_EPM_SPI_ADC2_CS_N (GPIO_ADC2_PWDN_N + 1)
153#define GPIO_ADC2_DRDY_N (GPIO_EPM_SPI_ADC2_CS_N + 1)
154
155static int epm_adc_i2c_expander_register(void)
156{
157 int rc = 0;
158 static struct i2c_adapter *i2c_adap;
159 static struct i2c_client *epm_i2c_client;
160
161 rc = gpio_request(GPIO_EPM_GLOBAL_ENABLE, "EPM_GLOBAL_EN");
162 if (!rc) {
163 gpio_direction_output(GPIO_EPM_GLOBAL_ENABLE, 1);
164 } else {
165 pr_err("%s: Configure EPM_GLOBAL_EN Failed\n", __func__);
166 return rc;
167 }
168
169 usleep_range(EPM_ADC_CONVERSION_TIME_MIN,
170 EPM_ADC_CONVERSION_TIME_MAX);
171
172 i2c_adap = i2c_get_adapter(epm_adc_drv->bus_id);
173 if (i2c_adap == NULL) {
174 pr_err("%s: i2c_get_adapter() failed\n", __func__);
175 return -EINVAL;
176 }
177
178 usleep_range(EPM_ADC_CONVERSION_TIME_MIN,
179 EPM_ADC_CONVERSION_TIME_MAX);
180
181 epm_i2c_client = i2c_new_device(i2c_adap, epm_i2c_info);
182 if (IS_ERR(epm_i2c_client)) {
183 pr_err("Error with i2c epm device register\n");
184 return -ENODEV;
185 }
186
187 epm_adc_first_request = false;
188
189 return 0;
190}
191
192static int epm_adc_gpio_configure_expander_enable(void)
193{
194 int rc = 0;
195
196 if (epm_adc_first_request) {
197 rc = gpio_request(GPIO_EPM_GLOBAL_ENABLE, "EPM_GLOBAL_EN");
198 if (!rc) {
199 gpio_direction_output(GPIO_EPM_GLOBAL_ENABLE, 1);
200 } else {
201 pr_err("%s: Configure EPM_GLOBAL_EN Failed\n",
202 __func__);
203 return rc;
204 }
205 } else {
206 epm_adc_first_request = true;
207 }
208
209 usleep_range(EPM_ADC_CONVERSION_TIME_MIN,
210 EPM_ADC_CONVERSION_TIME_MAX);
211
212 rc = gpio_request(GPIO_PWR_MON_ENABLE, "GPIO_PWR_MON_ENABLE");
213 if (!rc) {
214 rc = gpio_direction_output(GPIO_PWR_MON_ENABLE, 1);
215 if (rc) {
216 pr_err("%s: Set GPIO_PWR_MON_ENABLE failed\n",
217 __func__);
218 return rc;
219 }
220 } else {
221 pr_err("%s: gpio_request GPIO_PWR_MON_ENABLE failed\n",
222 __func__);
223 return rc;
224 }
225
226 rc = gpio_request(GPIO_ADC1_PWDN_N, "GPIO_ADC1_PWDN_N");
227 if (!rc) {
228 rc = gpio_direction_output(GPIO_ADC1_PWDN_N, 1);
229 if (rc) {
230 pr_err("%s: Set GPIO_ADC1_PWDN_N failed\n", __func__);
231 return rc;
232 }
233 } else {
234 pr_err("%s: gpio_request GPIO_ADC1_PWDN_N failed\n", __func__);
235 return rc;
236 }
237
238 rc = gpio_request(GPIO_ADC2_PWDN_N, "GPIO_ADC2_PWDN_N");
239 if (!rc) {
240 rc = gpio_direction_output(GPIO_ADC2_PWDN_N, 1);
241 if (rc) {
242 pr_err("%s: Set GPIO_ADC2_PWDN_N failed\n",
243 __func__);
244 return rc;
245 }
246 } else {
247 pr_err("%s: gpio_request GPIO_ADC2_PWDN_N failed\n",
248 __func__);
249 return rc;
250 }
251
252 rc = gpio_request(GPIO_EPM_SPI_ADC1_CS_N, "GPIO_EPM_SPI_ADC1_CS_N");
253 if (!rc) {
254 rc = gpio_direction_output(GPIO_EPM_SPI_ADC1_CS_N, 1);
255 if (rc) {
256 pr_err("%s:Set GPIO_EPM_SPI_ADC1_CS_N failed\n",
257 __func__);
258 return rc;
259 }
260 } else {
261 pr_err("%s: gpio_request GPIO_EPM_SPI_ADC1_CS_N failed\n",
262 __func__);
263 return rc;
264 }
265
266 rc = gpio_request(GPIO_EPM_SPI_ADC2_CS_N,
267 "GPIO_EPM_SPI_ADC2_CS_N");
268 if (!rc) {
269 rc = gpio_direction_output(GPIO_EPM_SPI_ADC2_CS_N, 1);
270 if (rc) {
Siddartha Mohanadossefddea42012-09-04 08:23:43 -0700271 pr_err("Set GPIO_EPM_SPI_ADC2_CS_N failed\n");
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700272 return rc;
273 }
274 } else {
Siddartha Mohanadossefddea42012-09-04 08:23:43 -0700275 pr_err("gpio_request GPIO_EPM_SPI_ADC2_CS_N failed\n");
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700276 return rc;
277 }
278
279 rc = gpio_direction_output(GPIO_EPM_SPI_ADC1_CS_N, 0);
280 if (rc) {
281 pr_err("%s:Reset GPIO_EPM_SPI_ADC1_CS_N failed\n", __func__);
282 return rc;
283 }
284
285 rc = gpio_direction_output(GPIO_EPM_SPI_ADC1_CS_N, 1);
286 if (rc) {
287 pr_err("%s: Set GPIO_EPM_SPI_ADC1_CS_N failed\n", __func__);
288 return rc;
289 }
290
291 rc = gpio_request(GPIO_PWR_MON_START, "GPIO_PWR_MON_START");
292 if (!rc) {
293 rc = gpio_direction_output(GPIO_PWR_MON_START, 0);
294 if (rc) {
295 pr_err("%s: Reset GPIO_PWR_MON_START failed\n",
296 __func__);
297 return rc;
298 }
299 } else {
300 pr_err("%s: gpio_request GPIO_PWR_MON_START failed\n",
301 __func__);
302 return rc;
303 }
304
305 rc = gpio_request(GPIO_PWR_MON_RESET_N, "GPIO_PWR_MON_RESET_N");
306 if (!rc) {
307 rc = gpio_direction_output(GPIO_PWR_MON_RESET_N, 0);
308 if (rc) {
309 pr_err("%s: Reset GPIO_PWR_MON_RESET_N failed\n",
310 __func__);
311 return rc;
312 }
313 } else {
314 pr_err("%s: gpio_request GPIO_PWR_MON_RESET_N failed\n",
315 __func__);
316 return rc;
317 }
318
319 rc = gpio_direction_output(GPIO_PWR_MON_RESET_N, 1);
320 if (rc) {
321 pr_err("%s: Set GPIO_PWR_MON_RESET_N failed\n", __func__);
322 return rc;
323 }
324
325 rc = gpio_direction_output(GPIO_EPM_SPI_ADC1_CS_N, 0);
326 if (rc) {
327 pr_err("%s:Reset GPIO_EPM_SPI_ADC1_CS_N failed\n", __func__);
328 return rc;
329 }
330 return rc;
331}
332
333static int epm_adc_gpio_configure_expander_disable(void)
334{
335 int rc = 0;
336 gpio_free(GPIO_PWR_MON_ENABLE);
337 gpio_free(GPIO_ADC1_PWDN_N);
338 gpio_free(GPIO_ADC2_PWDN_N);
339 gpio_free(GPIO_EPM_SPI_ADC1_CS_N);
340 gpio_free(GPIO_EPM_SPI_ADC2_CS_N);
341 gpio_free(GPIO_PWR_MON_START);
342 gpio_free(GPIO_PWR_MON_RESET_N);
343 rc = gpio_direction_output(GPIO_EPM_GLOBAL_ENABLE, 0);
344 if (rc)
345 pr_debug("%s: Disable EPM_GLOBAL_EN Failed\n", __func__);
346 gpio_free(GPIO_EPM_GLOBAL_ENABLE);
347 return rc;
348}
349
350static int epm_adc_spi_chip_select(int32_t id)
351{
352 int rc = 0;
353 if (id == 0) {
354 rc = gpio_direction_output(GPIO_EPM_SPI_ADC2_CS_N, 1);
355 if (rc) {
356 pr_err("%s:Disable SPI_ADC2_CS failed",
357 __func__);
358 return rc;
359 }
360
361 rc = gpio_direction_output(GPIO_EPM_SPI_ADC1_CS_N, 0);
362 if (rc) {
363 pr_err("%s:Enable SPI_ADC1_CS failed", __func__);
364 return rc;
365 }
366 } else if (id == 1) {
367 rc = gpio_direction_output(GPIO_EPM_SPI_ADC1_CS_N, 1);
368 if (rc) {
369 pr_err("%s:Disable SPI_ADC1_CS failed", __func__);
370 return rc;
371 }
372 rc = gpio_direction_output(GPIO_EPM_SPI_ADC2_CS_N, 0);
373 if (rc) {
374 pr_err("%s:Enable SPI_ADC2_CS failed", __func__);
375 return rc;
376 }
377 } else {
378 rc = -EFAULT;
379 }
380 return rc;
381}
382
383static int epm_adc_ads_spi_write(struct epm_adc_drv *epm_adc,
384 uint8_t addr, uint8_t val)
385{
386 struct spi_message m;
387 struct spi_transfer t;
388 char tx_buf[2];
389 int rc = 0;
390
391 spi_setup(epm_adc->epm_spi_client);
392
393 memset(&t, 0, sizeof t);
394 memset(tx_buf, 0, sizeof tx_buf);
395 t.tx_buf = tx_buf;
396 spi_message_init(&m);
397 spi_message_add_tail(&t, &m);
398
399 tx_buf[0] = EPM_ADC_ADS_REG_WRITE_CMD | addr;
400 tx_buf[1] = val;
401
402 t.len = sizeof(tx_buf);
403 t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD;
404
405 rc = spi_sync(epm_adc->epm_spi_client, &m);
406
407 return rc;
408}
409
410static int epm_adc_init_ads(struct epm_adc_drv *epm_adc)
411{
412 int rc = 0;
413
414 rc = epm_adc_ads_spi_write(epm_adc, EPM_ADC_ADS_CONFIG0_REG_ADDR,
415 EPM_ADC_ADS_REG0_DEFAULT);
416 if (rc)
417 return rc;
418
419 rc = epm_adc_ads_spi_write(epm_adc, EPM_ADC_ADS_CONFIG1_REG_ADDR,
420 EPM_ADC_ADS_REG1_DEFAULT);
421 if (rc)
422 return rc;
423 return rc;
424}
425
426static int epm_adc_ads_pulse_convert(struct epm_adc_drv *epm_adc)
427{
428 struct spi_message m;
429 struct spi_transfer t;
430 char tx_buf[1];
431 int rc = 0;
432
433 spi_setup(epm_adc->epm_spi_client);
434
435 memset(&t, 0, sizeof t);
436 memset(tx_buf, 0, sizeof tx_buf);
437 t.tx_buf = tx_buf;
438 spi_message_init(&m);
439 spi_message_add_tail(&t, &m);
440
441 tx_buf[0] = EPM_ADC_ADS_PULSE_CONVERT_CMD;
442 t.len = sizeof(tx_buf);
443 t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD;
444
445 rc = spi_sync(epm_adc->epm_spi_client, &m);
446
447 return rc;
448}
449
450static int epm_adc_ads_read_data(struct epm_adc_drv *epm_adc, char *adc_data)
451{
452 struct spi_message m;
453 struct spi_transfer t;
454 char tx_buf[4], rx_buf[4];
455 int rc = 0;
456
457 spi_setup(epm_adc->epm_spi_client);
458
459 memset(&t, 0, sizeof t);
460 memset(tx_buf, 0, sizeof tx_buf);
461 memset(rx_buf, 0, sizeof tx_buf);
462 t.tx_buf = tx_buf;
463 t.rx_buf = rx_buf;
464 spi_message_init(&m);
465 spi_message_add_tail(&t, &m);
466
467 tx_buf[0] = EPM_ADC_ADS_DATA_READ_CMD |
468 EPM_ADC_ADS_MULTIPLE_REG_ACCESS;
469
470 t.len = sizeof(tx_buf);
471 t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD;
472
473 rc = spi_sync(epm_adc->epm_spi_client, &m);
474 if (rc)
475 return rc;
476
477 rc = spi_sync(epm_adc->epm_spi_client, &m);
478 if (rc)
479 return rc;
480
481 rc = spi_sync(epm_adc->epm_spi_client, &m);
482 if (rc)
483 return rc;
484
485 adc_data[0] = rx_buf[1];
486 adc_data[1] = rx_buf[2];
487 adc_data[2] = rx_buf[3];
488
489 return rc;
490}
491
492static int epm_adc_hw_init(struct epm_adc_drv *epm_adc)
493{
494 int rc = 0;
495
496 mutex_lock(&epm_adc->conv_lock);
497 rc = epm_adc_gpio_configure_expander_enable();
498 if (rc != 0) {
499 pr_err("epm gpio configure expander failed, rc = %d\n", rc);
500 goto epm_adc_hw_init_err;
501 }
502 rc = epm_adc_init_ads(epm_adc);
503 if (rc) {
504 pr_err("epm_adc_init_ads failed, rc=%d\n", rc);
505 goto epm_adc_hw_init_err;
506 }
507
508epm_adc_hw_init_err:
509 mutex_unlock(&epm_adc->conv_lock);
510 return rc;
511}
512
513static int epm_adc_hw_deinit(struct epm_adc_drv *epm_adc)
514{
515 int rc = 0;
516
517 mutex_lock(&epm_adc->conv_lock);
518 rc = epm_adc_gpio_configure_expander_disable();
519 if (rc != 0) {
Siddartha Mohanadossefddea42012-09-04 08:23:43 -0700520 pr_err("gpio expander disable failed with %d\n", rc);
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700521 goto epm_adc_hw_deinit_err;
522 }
523
524epm_adc_hw_deinit_err:
525 mutex_unlock(&epm_adc->conv_lock);
526 return rc;
527}
528
529static int epm_adc_ads_scale_result(struct epm_adc_drv *epm_adc,
530 uint8_t *adc_raw_data, struct epm_chan_request *conv)
531{
532 uint32_t channel_num;
533 int16_t sign_bit;
534 struct epm_adc_platform_data *pdata = epm_adc->pdev->dev.platform_data;
535 uint32_t chan_idx = (conv->device_idx * pdata->chan_per_adc) +
536 conv->channel_idx;
Siddartha Mohanadossde90ed42012-09-18 16:57:50 -0700537 int64_t adc_scaled_data = 0;
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700538
539 /* Get the channel number */
540 channel_num = (adc_raw_data[0] & EPM_ADC_ADS_CHANNEL_DATA_CHID);
541 sign_bit = 1;
542 /* This is the 16-bit raw data */
Siddartha Mohanadossde90ed42012-09-18 16:57:50 -0700543 adc_scaled_data = ((adc_raw_data[1] << 8) | adc_raw_data[2]);
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700544 /* Obtain the internal system reading */
545 if (channel_num == EPM_ADC_ADS_CHANNEL_VCC) {
Siddartha Mohanadossde90ed42012-09-18 16:57:50 -0700546 adc_scaled_data *= EPM_ADC_SCALE_MILLI;
547 do_div(adc_scaled_data, EPM_ADC_SCALE_CODE_VOLTS);
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700548 } else if (channel_num == EPM_ADC_ADS_CHANNEL_GAIN) {
Siddartha Mohanadossde90ed42012-09-18 16:57:50 -0700549 do_div(adc_scaled_data, EPM_ADC_SCALE_CODE_GAIN);
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700550 } else if (channel_num == EPM_ADC_ADS_CHANNEL_REF) {
Siddartha Mohanadossde90ed42012-09-18 16:57:50 -0700551 adc_scaled_data *= EPM_ADC_SCALE_MILLI;
552 do_div(adc_scaled_data, EPM_ADC_SCALE_CODE_VOLTS);
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700553 } else if (channel_num == EPM_ADC_ADS_CHANNEL_TEMP) {
554 /* Convert Code to micro-volts */
555 /* Use this formula to get the temperature reading */
Siddartha Mohanadossde90ed42012-09-18 16:57:50 -0700556 adc_scaled_data -= EPM_ADC_TEMP_TO_DEGC_COEFF;
557 do_div(adc_scaled_data, EPM_ADC_TEMP_SENSOR_COEFF);
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700558 } else if (channel_num == EPM_ADC_ADS_CHANNEL_OFFSET) {
559 /* The offset should be zero */
560 pr_debug("%s: ADC Channel Offset\n", __func__);
561 return -EFAULT;
562 } else {
563 channel_num -= EPM_ADC_CHANNEL_AIN_OFFSET;
564 /*
565 * Conversion for the adc channels.
Siddartha Mohanadossefddea42012-09-04 08:23:43 -0700566 * mvVRef is in milli-volts and resistorvalue is in micro-ohms.
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700567 * Hence, I = V/R gives us current in kilo-amps.
568 */
Siddartha Mohanadossde90ed42012-09-18 16:57:50 -0700569 if (adc_scaled_data & EPM_ADC_MAX_NEGATIVE_SCALE_CODE) {
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700570 sign_bit = -1;
Siddartha Mohanadossde90ed42012-09-18 16:57:50 -0700571 adc_scaled_data = (~adc_scaled_data
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700572 & EPM_ADC_NEG_LSB_CODE);
573 }
Siddartha Mohanadossde90ed42012-09-18 16:57:50 -0700574 if (adc_scaled_data != 0) {
575 adc_scaled_data *= EPM_ADC_SCALE_FACTOR;
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700576 /* Device is calibrated for 1LSB = VREF/7800h.*/
Siddartha Mohanadossde90ed42012-09-18 16:57:50 -0700577 adc_scaled_data *= EPM_ADC_MILLI_VOLTS_SOURCE;
578 do_div(adc_scaled_data, EPM_ADC_VREF_CODE);
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700579 /* Data will now be in micro-volts.*/
Siddartha Mohanadossde90ed42012-09-18 16:57:50 -0700580 adc_scaled_data *= EPM_ADC_SCALE_MILLI;
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700581 /* Divide by amplifier gain value.*/
Siddartha Mohanadossde90ed42012-09-18 16:57:50 -0700582 do_div(adc_scaled_data, pdata->channel[chan_idx].gain);
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700583 /* Data will now be in nano-volts.*/
Siddartha Mohanadossde90ed42012-09-18 16:57:50 -0700584 do_div(adc_scaled_data, EPM_ADC_SCALE_FACTOR);
585 adc_scaled_data *= EPM_ADC_SCALE_MILLI;
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700586 /* Data is now in micro-amps.*/
Siddartha Mohanadossde90ed42012-09-18 16:57:50 -0700587 do_div(adc_scaled_data,
Siddartha Mohanadossefddea42012-09-04 08:23:43 -0700588 pdata->channel[chan_idx].resistorvalue);
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700589 /* Set the sign bit for lekage current. */
Siddartha Mohanadossde90ed42012-09-18 16:57:50 -0700590 adc_scaled_data *= sign_bit;
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700591 }
592 }
Siddartha Mohanadoss6e77b772012-09-19 21:58:13 -0700593
Siddartha Mohanadossde90ed42012-09-18 16:57:50 -0700594 conv->physical = (int32_t) adc_scaled_data;
Yan Hef6adab72012-08-31 11:09:36 -0700595
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700596 return 0;
597}
598
Siddartha Mohanadoss899173a2013-01-29 19:18:50 -0800599static int epm_psoc_scale_result(int16_t result, uint32_t index)
Siddartha Mohanadossefddea42012-09-04 08:23:43 -0700600{
601 struct epm_adc_drv *epm_adc = epm_adc_drv;
Siddartha Mohanadoss899173a2013-01-29 19:18:50 -0800602 int32_t result_cur, neg = 0;
Siddartha Mohanadoss6e77b772012-09-19 21:58:13 -0700603
Siddartha Mohanadossae7acf82012-12-12 10:36:07 -0800604 if ((1 << index) & epm_adc->channel_mask) {
Siddartha Mohanadoss899173a2013-01-29 19:18:50 -0800605 if (result & 0x800) {
606 neg = 1;
607 result = result & 0x7ff;
608 }
Siddartha Mohanadossae7acf82012-12-12 10:36:07 -0800609 /* result = (2.048V * code)/(4096 * gain * rsense) */
610 result_cur = ((EPM_PSOC_VREF_VOLTAGE * result)/
611 EPM_PSOC_MAX_ADC_CODE_12_BIT);
Siddartha Mohanadoss6e77b772012-09-19 21:58:13 -0700612
Siddartha Mohanadossae7acf82012-12-12 10:36:07 -0800613 result_cur = (result_cur/
614 (epm_adc->epm_psoc_ch_prop[index].gain *
615 epm_adc->epm_psoc_ch_prop[index].resistorvalue));
Siddartha Mohanadoss899173a2013-01-29 19:18:50 -0800616 if (neg)
617 result_cur -= result_cur;
Siddartha Mohanadossae7acf82012-12-12 10:36:07 -0800618 } else {
Siddartha Mohanadoss899173a2013-01-29 19:18:50 -0800619 if (result & 0x8000) {
620 neg = 1;
621 result = result & 0x7fff;
622 }
Siddartha Mohanadossae7acf82012-12-12 10:36:07 -0800623 /* result = (2.048V * code)/(32767 * gain * rsense) */
Siddartha Mohanadoss899173a2013-01-29 19:18:50 -0800624 result_cur = (((EPM_PSOC_VREF_VOLTAGE * (int) result)/
Siddartha Mohanadossae7acf82012-12-12 10:36:07 -0800625 EPM_PSOC_MAX_ADC_CODE_15_BIT) * 1000);
626
627 result_cur = (result_cur/
Siddartha Mohanadossefddea42012-09-04 08:23:43 -0700628 (epm_adc->epm_psoc_ch_prop[index].gain *
Siddartha Mohanadoss6e77b772012-09-19 21:58:13 -0700629 epm_adc->epm_psoc_ch_prop[index].resistorvalue));
Siddartha Mohanadoss899173a2013-01-29 19:18:50 -0800630 if (neg)
631 result_cur -= result_cur;
Siddartha Mohanadossae7acf82012-12-12 10:36:07 -0800632 }
Siddartha Mohanadoss6e77b772012-09-19 21:58:13 -0700633
634 return result_cur;
Siddartha Mohanadossefddea42012-09-04 08:23:43 -0700635}
636
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -0700637static int epm_adc_blocking_conversion(struct epm_adc_drv *epm_adc,
638 struct epm_chan_request *conv)
639{
640 struct epm_adc_platform_data *pdata = epm_adc->pdev->dev.platform_data;
641 int32_t channel_num = 0, mux_chan_idx = 0;
642 char adc_data[3];
643 int rc = 0;
644
645 mutex_lock(&epm_adc->conv_lock);
646
647 rc = epm_adc_spi_chip_select(conv->device_idx);
648 if (rc) {
649 pr_err("epm_adc_chip_select failed, rc=%d\n", rc);
650 goto conv_err;
651 }
652
653 if (conv->channel_idx < pdata->chan_per_mux) {
654 /* Reset MUXSG1_REGISTER */
655 rc = epm_adc_ads_spi_write(epm_adc, EPM_ADC_ADS_MUXSG1_REG_ADDR,
656 0x0);
657 if (rc)
658 goto conv_err;
659
660 mux_chan_idx = 1 << conv->channel_idx;
661 /* Select Channel index in MUXSG0_REGISTER */
662 rc = epm_adc_ads_spi_write(epm_adc, EPM_ADC_ADS_MUXSG0_REG_ADDR,
663 mux_chan_idx);
664 if (rc)
665 goto conv_err;
666 } else {
667 /* Reset MUXSG0_REGISTER */
668 rc = epm_adc_ads_spi_write(epm_adc, EPM_ADC_ADS_MUXSG0_REG_ADDR,
669 0x0);
670 if (rc)
671 goto conv_err;
672
673 mux_chan_idx = 1 << (conv->channel_idx - pdata->chan_per_mux);
674 /* Select Channel index in MUXSG1_REGISTER */
675 rc = epm_adc_ads_spi_write(epm_adc, EPM_ADC_ADS_MUXSG1_REG_ADDR,
676 mux_chan_idx);
677 if (rc)
678 goto conv_err;
679 }
680
681 rc = epm_adc_ads_pulse_convert(epm_adc);
682 if (rc) {
683 pr_err("epm_adc_ads_pulse_convert failed, rc=%d\n", rc);
684 goto conv_err;
685 }
686
687 rc = epm_adc_ads_read_data(epm_adc, adc_data);
688 if (rc) {
689 pr_err("epm_adc_ads_read_data failed, rc=%d\n", rc);
690 goto conv_err;
691 }
692
693 channel_num = (adc_data[0] & EPM_ADC_ADS_CHANNEL_DATA_CHID);
694 pr_debug("ADC data Read: adc_data =%d, %d, %d\n",
695 adc_data[0], adc_data[1], adc_data[2]);
696
697 epm_adc_ads_scale_result(epm_adc, (uint8_t *)adc_data, conv);
698
699 pr_debug("channel_num(0x) = %x, scaled_data = %d\n",
700 (channel_num - EPM_ADC_ADS_SPI_BITS_PER_WORD),
701 conv->physical);
702conv_err:
703 mutex_unlock(&epm_adc->conv_lock);
704 return rc;
705}
706
Siddartha Mohanadossefddea42012-09-04 08:23:43 -0700707static int epm_adc_psoc_gpio_init(bool enable)
708{
709 int rc = 0;
710
711 if (enable) {
712 rc = gpio_request(EPM_PSOC_GLOBAL_ENABLE, "EPM_PSOC_GLOBAL_EN");
713 if (!rc) {
714 gpio_direction_output(EPM_PSOC_GLOBAL_ENABLE, 1);
715 } else {
716 pr_err("%s: Configure EPM_GLOBAL_EN Failed\n",
717 __func__);
718 return rc;
719 }
720 } else {
721 gpio_direction_output(EPM_PSOC_GLOBAL_ENABLE, 0);
722 gpio_free(EPM_PSOC_GLOBAL_ENABLE);
723 }
724
725 return 0;
726}
727
Siddartha Mohanadoss3efc4d82013-06-11 15:34:52 -0700728static int epm_set_marker1(struct epm_marker_level *marker_init)
729{
730 int rc = 0;
731
732 rc = gpio_request(GPIO_EPM_MARKER1, "EPM_MARKER1");
733 if (!rc) {
734 gpio_direction_output(GPIO_EPM_MARKER1, 1);
735 } else {
736 pr_err("%s: Configure MARKER1 GPIO Failed\n",
737 __func__);
738 return rc;
739 }
740
741 gpio_set_value(GPIO_EPM_MARKER1, marker_init->level);
742
743 return 0;
744}
745
746static int epm_set_marker2(struct epm_marker_level *marker_init)
747{
748 int rc = 0;
749
750 rc = gpio_request(GPIO_EPM_MARKER2, "EPM_MARKER2");
751 if (!rc) {
752 gpio_direction_output(GPIO_EPM_MARKER2, 1);
753 } else {
754 pr_err("%s: Configure MARKER2 GPIO Failed\n",
755 __func__);
756 return rc;
757 }
758
759 gpio_set_value(GPIO_EPM_MARKER2, marker_init->level);
760
761 return 0;
762}
763
764static int epm_marker1_release(void)
765{
766 gpio_free(GPIO_EPM_MARKER1);
767
768 return 0;
769}
770
771static int epm_marker2_release(void)
772{
773 gpio_free(GPIO_EPM_MARKER2);
774
775 return 0;
776}
777
Siddartha Mohanadoss46aff912013-07-10 18:53:48 -0700778static int epm_psoc_pause_conversion(struct epm_adc_drv *epm_adc)
779{
780 struct spi_message m;
781 struct spi_transfer t;
782 char tx_buf[2], rx_buf[2];
783 int rc = 0;
784
785 spi_setup(epm_adc->epm_spi_client);
786
787 memset(&t, 0, sizeof t);
788 memset(tx_buf, 0, sizeof tx_buf);
789 memset(rx_buf, 0, sizeof tx_buf);
790 t.tx_buf = tx_buf;
791 t.rx_buf = rx_buf;
792 spi_message_init(&m);
793 spi_message_add_tail(&t, &m);
794
795 tx_buf[0] = EPM_PSOC_PAUSE_CONVERSION;
796
797 t.len = sizeof(tx_buf);
798 t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD;
799
800 rc = spi_sync(epm_adc->epm_spi_client, &m);
801 if (rc) {
802 pr_err("spi sync err with %d\n", rc);
803 return rc;
804 }
805
806 rc = spi_sync(epm_adc->epm_spi_client, &m);
807 if (rc) {
808 pr_err("spi sync err with %d\n", rc);
809 return rc;
810 }
811
812 return rx_buf[0];
813}
814
815static int epm_psoc_unpause_conversion(struct epm_adc_drv *epm_adc)
816{
817 struct spi_message m;
818 struct spi_transfer t;
819 char tx_buf[2], rx_buf[2];
820 int rc = 0;
821
822 spi_setup(epm_adc->epm_spi_client);
823
824 memset(&t, 0, sizeof t);
825 memset(tx_buf, 0, sizeof tx_buf);
826 memset(rx_buf, 0, sizeof tx_buf);
827 t.tx_buf = tx_buf;
828 t.rx_buf = rx_buf;
829 spi_message_init(&m);
830 spi_message_add_tail(&t, &m);
831
832 tx_buf[0] = EPM_PSOC_UNPAUSE_CONVERSION;
833
834 t.len = sizeof(tx_buf);
835 t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD;
836
837 rc = spi_sync(epm_adc->epm_spi_client, &m);
838 if (rc) {
839 pr_err("spi sync err with %d\n", rc);
840 return rc;
841 }
842
843 rc = spi_sync(epm_adc->epm_spi_client, &m);
844 if (rc) {
845 pr_err("spi sync err with %d\n", rc);
846 return rc;
847 }
848
849 return rx_buf[0];
850}
851
Siddartha Mohanadossefddea42012-09-04 08:23:43 -0700852static int epm_psoc_init(struct epm_adc_drv *epm_adc,
853 struct epm_psoc_init_resp *init_resp)
854{
855 struct spi_message m;
856 struct spi_transfer t;
857 char tx_buf[17], rx_buf[17];
858 int rc = 0;
859
860 spi_setup(epm_adc->epm_spi_client);
861
862 memset(&t, 0, sizeof t);
863 memset(tx_buf, 0, sizeof tx_buf);
864 memset(rx_buf, 0, sizeof tx_buf);
865 t.tx_buf = tx_buf;
866 t.rx_buf = rx_buf;
867 spi_message_init(&m);
868 spi_message_add_tail(&t, &m);
869
870 tx_buf[0] = init_resp->cmd;
871
872 t.len = sizeof(tx_buf);
873 t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD;
874
875 rc = spi_sync(epm_adc->epm_spi_client, &m);
876 if (rc)
877 return rc;
878
Siddartha Mohanadoss6e77b772012-09-19 21:58:13 -0700879 rc = spi_sync(epm_adc->epm_spi_client, &m);
880 if (rc)
881 return rc;
Siddartha Mohanadossefddea42012-09-04 08:23:43 -0700882 init_resp->cmd = rx_buf[0];
883 init_resp->version = rx_buf[1];
884 init_resp->compatible_ver = rx_buf[2];
885 init_resp->firm_ver[0] = rx_buf[3];
886 init_resp->firm_ver[1] = rx_buf[4];
887 init_resp->firm_ver[2] = rx_buf[5];
888 init_resp->num_dev = rx_buf[6];
889 init_resp->num_channel = rx_buf[7];
890
Siddartha Mohanadosse845f072013-12-18 17:14:11 -0800891 pr_debug("EPM PSOC response for hello command: resp_cmd:0x%x\n",
892 rx_buf[0]);
893 pr_debug("EPM PSOC version:0x%x\n", rx_buf[1]);
894 pr_debug("EPM PSOC firmware version:0x%x\n",
895 rx_buf[6] | rx_buf[5] | rx_buf[4] | rx_buf[3]);
896
Siddartha Mohanadossefddea42012-09-04 08:23:43 -0700897 return rc;
898}
899
900static int epm_psoc_channel_configure(struct epm_adc_drv *epm_adc,
901 struct epm_psoc_channel_configure *psoc_chan_configure)
902{
903 struct spi_message m;
904 struct spi_transfer t;
905 char tx_buf[9], rx_buf[9];
906 int32_t rc = 0, chan_num;
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 chan_num = psoc_chan_configure->channel_num;
919
920 tx_buf[0] = psoc_chan_configure->cmd;
921 tx_buf[1] = 0;
922 tx_buf[2] = (chan_num & 0xff000000) >> 24;
923 tx_buf[3] = (chan_num & 0xff0000) >> 16;
924 tx_buf[4] = (chan_num & 0xff00) >> 8;
925 tx_buf[5] = (chan_num & 0xff);
926
927 t.len = sizeof(tx_buf);
928 t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD;
929
930 rc = spi_sync(epm_adc->epm_spi_client, &m);
931 if (rc)
932 return rc;
933
934 rc = spi_sync(epm_adc->epm_spi_client, &m);
935 if (rc)
936 return rc;
937
938 psoc_chan_configure->cmd = rx_buf[0];
939 psoc_chan_configure->device_num = rx_buf[1];
940 chan_num = rx_buf[2] << 24 | (rx_buf[3] << 16) | (rx_buf[4] << 8) |
941 rx_buf[5];
942 psoc_chan_configure->channel_num = chan_num;
Siddartha Mohanadossefddea42012-09-04 08:23:43 -0700943
944 return rc;
945}
946
947static int epm_psoc_set_averaging(struct epm_adc_drv *epm_adc,
948 struct epm_psoc_set_avg *psoc_set_avg)
949{
950 struct spi_message m;
951 struct spi_transfer t;
952 char tx_buf[4], rx_buf[4];
953 int rc = 0;
954
955 spi_setup(epm_adc->epm_spi_client);
956
957 memset(&t, 0, sizeof t);
958 memset(tx_buf, 0, sizeof tx_buf);
959 memset(rx_buf, 0, sizeof tx_buf);
960 t.tx_buf = tx_buf;
961 t.rx_buf = rx_buf;
962 spi_message_init(&m);
963 spi_message_add_tail(&t, &m);
964
965 tx_buf[0] = psoc_set_avg->cmd;
966 tx_buf[1] = psoc_set_avg->avg_period;
967
968 t.len = sizeof(tx_buf);
969 t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD;
970
971 rc = spi_sync(epm_adc->epm_spi_client, &m);
972 if (rc)
973 return rc;
974
975 rc = spi_sync(epm_adc->epm_spi_client, &m);
976 if (rc)
977 return rc;
978
979 psoc_set_avg->cmd = rx_buf[0];
980 psoc_set_avg->return_code = rx_buf[1];
981
982 return rc;
983}
984
985static int epm_psoc_get_data(struct epm_adc_drv *epm_adc,
986 struct epm_psoc_get_data *psoc_get_meas)
987{
988 struct spi_message m;
989 struct spi_transfer t;
990 char tx_buf[10], rx_buf[10];
991 int rc = 0;
992
993 spi_setup(epm_adc->epm_spi_client);
994
995 memset(&t, 0, sizeof t);
996 memset(tx_buf, 0, sizeof tx_buf);
997 memset(rx_buf, 0, sizeof tx_buf);
998 t.tx_buf = tx_buf;
999 t.rx_buf = rx_buf;
1000 spi_message_init(&m);
1001 spi_message_add_tail(&t, &m);
1002
1003 tx_buf[0] = psoc_get_meas->cmd;
1004 tx_buf[1] = psoc_get_meas->dev_num;
1005 tx_buf[2] = psoc_get_meas->chan_num;
1006
1007 t.len = sizeof(tx_buf);
1008 t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD;
1009
1010 rc = spi_sync(epm_adc->epm_spi_client, &m);
1011 if (rc)
1012 return rc;
1013
1014 rc = spi_sync(epm_adc->epm_spi_client, &m);
1015 if (rc)
1016 return rc;
1017
1018 psoc_get_meas->cmd = rx_buf[0];
1019 psoc_get_meas->dev_num = rx_buf[1];
1020 psoc_get_meas->chan_num = rx_buf[2];
1021 psoc_get_meas->timestamp_resp_value = (rx_buf[3] << 24) |
1022 (rx_buf[4] << 16) | (rx_buf[5] << 8) |
1023 rx_buf[6];
Siddartha Mohanadoss899173a2013-01-29 19:18:50 -08001024 psoc_get_meas->reading_raw = (rx_buf[7] << 8) | rx_buf[8];
Siddartha Mohanadossefddea42012-09-04 08:23:43 -07001025
Siddartha Mohanadossefddea42012-09-04 08:23:43 -07001026 return rc;
1027}
1028
1029static int epm_psoc_get_buffered_data(struct epm_adc_drv *epm_adc,
1030 struct epm_psoc_get_buffered_data *psoc_get_meas)
1031{
1032 struct spi_message m;
1033 struct spi_transfer t;
1034 char tx_buf[64], rx_buf[64];
1035 int rc = 0, i;
1036
1037 spi_setup(epm_adc->epm_spi_client);
1038
1039 memset(&t, 0, sizeof t);
1040 memset(tx_buf, 0, sizeof tx_buf);
1041 memset(rx_buf, 0, sizeof tx_buf);
1042 t.tx_buf = tx_buf;
1043 t.rx_buf = rx_buf;
1044 spi_message_init(&m);
1045 spi_message_add_tail(&t, &m);
1046
1047 tx_buf[0] = psoc_get_meas->cmd;
1048
1049 t.len = sizeof(tx_buf);
1050 t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD;
1051
1052 rc = spi_sync(epm_adc->epm_spi_client, &m);
1053 if (rc)
1054 return rc;
1055
1056 rc = spi_sync(epm_adc->epm_spi_client, &m);
1057 if (rc)
1058 return rc;
1059
1060 psoc_get_meas->cmd = rx_buf[0];
1061 psoc_get_meas->dev_num = rx_buf[1];
1062 psoc_get_meas->status_mask = rx_buf[2];
1063 psoc_get_meas->chan_idx = rx_buf[3];
1064 psoc_get_meas->chan_mask = (rx_buf[4] << 24 |
1065 rx_buf[5] << 16 | rx_buf[6] << 8
1066 | rx_buf[7]);
1067 psoc_get_meas->timestamp_start = (rx_buf[8] << 24 |
1068 rx_buf[9] << 16 | rx_buf[10] << 8
1069 | rx_buf[11]);
1070 psoc_get_meas->timestamp_end = (rx_buf[12] << 24 |
1071 rx_buf[13] << 16 | rx_buf[14] << 8
1072 | rx_buf[15]);
1073
1074 for (i = 0; i < EPM_PSOC_BUFFERED_DATA_LENGTH; i++)
1075 psoc_get_meas->buff_data[i] = rx_buf[16 + i];
1076
1077 return rc;
1078}
1079
Siddartha Mohanadoss278815e2013-04-22 11:18:17 -07001080static int epm_psoc_get_timestamp(struct epm_adc_drv *epm_adc,
Siddartha Mohanadossefddea42012-09-04 08:23:43 -07001081 struct epm_psoc_system_time_stamp *psoc_timestamp)
1082{
1083 struct spi_message m;
1084 struct spi_transfer t;
1085 char tx_buf[10], rx_buf[10];
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
Siddartha Mohanadoss278815e2013-04-22 11:18:17 -07001098 psoc_timestamp->cmd = EPM_PSOC_GET_SYSTEM_TIMESTAMP_CMD;
1099 tx_buf[0] = psoc_timestamp->cmd;
1100
1101 t.len = sizeof(tx_buf);
1102 t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD;
1103
1104 rc = spi_sync(epm_adc->epm_spi_client, &m);
1105 if (rc)
1106 return rc;
1107
1108 rc = spi_sync(epm_adc->epm_spi_client, &m);
1109 if (rc)
1110 return rc;
1111
1112 psoc_timestamp->cmd = rx_buf[0];
1113 psoc_timestamp->timestamp = rx_buf[1] << 24 | rx_buf[2] << 16 |
1114 rx_buf[3] << 8 | rx_buf[4];
1115
1116 return rc;
1117}
1118
1119static int epm_psoc_set_timestamp(struct epm_adc_drv *epm_adc,
1120 struct epm_psoc_system_time_stamp *psoc_timestamp)
1121{
1122 struct spi_message m;
1123 struct spi_transfer t;
1124 char tx_buf[10], rx_buf[10];
1125 int rc = 0;
1126
1127 spi_setup(epm_adc->epm_spi_client);
1128
1129 memset(&t, 0, sizeof t);
1130 memset(tx_buf, 0, sizeof tx_buf);
1131 memset(rx_buf, 0, sizeof tx_buf);
1132 t.tx_buf = tx_buf;
1133 t.rx_buf = rx_buf;
1134 spi_message_init(&m);
1135 spi_message_add_tail(&t, &m);
1136
1137 psoc_timestamp->cmd = EPM_PSOC_SET_SYSTEM_TIMESTAMP_CMD;
1138 tx_buf[0] = psoc_timestamp->cmd;
1139 tx_buf[1] = (psoc_timestamp->timestamp >> 24) & 0xff;
1140 tx_buf[2] = (psoc_timestamp->timestamp >> 16) & 0xff;
1141 tx_buf[3] = (psoc_timestamp->timestamp >> 8) & 0xff;
1142 tx_buf[4] = (psoc_timestamp->timestamp & 0xff);
Siddartha Mohanadossefddea42012-09-04 08:23:43 -07001143
1144 t.len = sizeof(tx_buf);
1145 t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD;
1146
1147 rc = spi_sync(epm_adc->epm_spi_client, &m);
1148 if (rc)
1149 return rc;
1150
1151 rc = spi_sync(epm_adc->epm_spi_client, &m);
1152 if (rc)
1153 return rc;
1154
1155 psoc_timestamp->cmd = rx_buf[0];
1156 psoc_timestamp->timestamp = rx_buf[1] << 24 | rx_buf[2] << 16 |
1157 rx_buf[3] << 8 | rx_buf[4];
1158
1159 return rc;
1160}
1161
1162static int epm_psoc_get_avg_buffered_switch_data(struct epm_adc_drv *epm_adc,
1163 struct epm_psoc_get_avg_buffered_switch_data *psoc_get_meas)
1164{
1165 struct spi_message m;
1166 struct spi_transfer t;
1167 char tx_buf[64], rx_buf[64];
Siddartha Mohanadoss6e77b772012-09-19 21:58:13 -07001168 int rc = 0, i = 0, j = 0, z = 0;
Siddartha Mohanadossefddea42012-09-04 08:23:43 -07001169
1170 spi_setup(epm_adc->epm_spi_client);
1171
1172 memset(&t, 0, sizeof t);
1173 memset(tx_buf, 0, sizeof tx_buf);
1174 memset(rx_buf, 0, sizeof tx_buf);
1175 t.tx_buf = tx_buf;
1176 t.rx_buf = rx_buf;
1177 spi_message_init(&m);
1178 spi_message_add_tail(&t, &m);
1179
1180 tx_buf[0] = psoc_get_meas->cmd;
1181
1182 t.len = sizeof(tx_buf);
1183 t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD;
1184
1185 rc = spi_sync(epm_adc->epm_spi_client, &m);
1186 if (rc)
1187 return rc;
1188
1189 rc = spi_sync(epm_adc->epm_spi_client, &m);
1190 if (rc)
1191 return rc;
1192
1193 psoc_get_meas->cmd = rx_buf[0];
1194 psoc_get_meas->status = rx_buf[1];
1195 psoc_get_meas->timestamp_start = (rx_buf[2] << 24 |
1196 rx_buf[3] << 16 | rx_buf[4] << 8
1197 | rx_buf[5]);
1198 psoc_get_meas->channel_mask = (rx_buf[6] << 24 |
1199 rx_buf[7] << 16 | rx_buf[8] << 8
1200 | rx_buf[9]);
1201
1202 for (i = 0; i < EPM_PSOC_BUFFERED_DATA_LENGTH2; i++)
1203 psoc_get_meas->avg_data[i] = rx_buf[10 + i];
1204
Siddartha Mohanadoss6e77b772012-09-19 21:58:13 -07001205 i = j = 0;
1206 for (z = 0; z < 4; z++) {
1207 psoc_get_meas->data[i].channel = i;
1208 psoc_get_meas->data[i].avg_buffer_sample =
1209 rx_buf[10 + j] & EPM_AVG_BUF_MASK1;
1210 i++;
1211 j++;
1212 psoc_get_meas->data[i].avg_buffer_sample =
1213 rx_buf[10 + j] & EPM_AVG_BUF_MASK2;
1214 i++;
1215 j++;
1216 psoc_get_meas->data[i].avg_buffer_sample =
1217 rx_buf[10 + j] & EPM_AVG_BUF_MASK3;
1218 psoc_get_meas->data[i].avg_buffer_sample <<= 8;
1219 j++;
1220 psoc_get_meas->data[i].avg_buffer_sample =
1221 psoc_get_meas->data[i].avg_buffer_sample |
1222 (rx_buf[10 + j] & EPM_AVG_BUF_MASK4);
1223 i++;
1224 j++;
1225 psoc_get_meas->data[i].avg_buffer_sample =
1226 rx_buf[10 + j] & EPM_AVG_BUF_MASK5;
1227 i++;
1228 j++;
1229 psoc_get_meas->data[i].avg_buffer_sample =
1230 rx_buf[10 + j] & EPM_AVG_BUF_MASK6;
1231 i++;
1232 j++;
1233 psoc_get_meas->data[i].avg_buffer_sample =
1234 rx_buf[10 + j] & EPM_AVG_BUF_MASK7;
1235 psoc_get_meas->data[i].avg_buffer_sample <<= 4;
1236 j++;
1237 psoc_get_meas->data[i].avg_buffer_sample =
1238 psoc_get_meas->data[i].avg_buffer_sample |
1239 (rx_buf[10 + j] & EPM_AVG_BUF_MASK8);
1240 i++;
1241 j++;
1242 psoc_get_meas->data[i].avg_buffer_sample =
1243 rx_buf[10 + j] & EPM_AVG_BUF_MASK9;
1244 i++;
1245 j++;
1246 psoc_get_meas->data[i].avg_buffer_sample =
1247 rx_buf[10 + j] & EPM_AVG_BUF_MASK10;
1248 }
1249
1250 for (z = 0; z < 32; z++) {
1251 if (psoc_get_meas->data[z].avg_buffer_sample != 0)
1252 psoc_get_meas->data[z].result = epm_psoc_scale_result(
1253 psoc_get_meas->data[z].avg_buffer_sample, z);
1254 }
1255
Siddartha Mohanadossefddea42012-09-04 08:23:43 -07001256 return rc;
1257}
1258
1259static int epm_psoc_set_vadc(struct epm_adc_drv *epm_adc,
1260 struct epm_psoc_set_vadc *psoc_set_vadc)
1261{
1262 struct spi_message m;
1263 struct spi_transfer t;
1264 char tx_buf[10], rx_buf[10];
1265 int rc = 0;
1266
1267 spi_setup(epm_adc->epm_spi_client);
1268
1269 memset(&t, 0, sizeof t);
1270 memset(tx_buf, 0, sizeof tx_buf);
1271 memset(rx_buf, 0, sizeof tx_buf);
1272 t.tx_buf = tx_buf;
1273 t.rx_buf = rx_buf;
1274 spi_message_init(&m);
1275 spi_message_add_tail(&t, &m);
1276
1277 tx_buf[0] = psoc_set_vadc->cmd;
1278 tx_buf[1] = psoc_set_vadc->vadc_dev;
1279 tx_buf[2] = (psoc_set_vadc->vadc_voltage & 0xff000000) >> 24;
1280 tx_buf[3] = (psoc_set_vadc->vadc_voltage & 0xff0000) >> 16;
1281 tx_buf[4] = (psoc_set_vadc->vadc_voltage & 0xff00) >> 8;
1282 tx_buf[5] = psoc_set_vadc->vadc_voltage & 0xff;
1283
1284 t.len = sizeof(tx_buf);
1285 t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD;
1286
1287 rc = spi_sync(epm_adc->epm_spi_client, &m);
1288 if (rc)
1289 return rc;
1290
1291 rc = spi_sync(epm_adc->epm_spi_client, &m);
1292 if (rc)
1293 return rc;
1294
1295 psoc_set_vadc->cmd = rx_buf[0];
1296 psoc_set_vadc->vadc_dev = rx_buf[1];
1297 psoc_set_vadc->vadc_voltage = (rx_buf[2] << 24) | (rx_buf[3] << 16) |
1298 (rx_buf[4] << 8) | (rx_buf[5]);
1299
1300 return rc;
1301}
1302
1303static int epm_psoc_set_channel_switch(struct epm_adc_drv *epm_adc,
1304 struct epm_psoc_set_channel_switch *psoc_channel_switch)
1305{
1306 struct spi_message m;
1307 struct spi_transfer t;
1308 char tx_buf[10], rx_buf[10];
1309 int rc = 0;
1310
1311 spi_setup(epm_adc->epm_spi_client);
1312
1313 memset(&t, 0, sizeof t);
1314 memset(tx_buf, 0, sizeof tx_buf);
1315 memset(rx_buf, 0, sizeof tx_buf);
1316 t.tx_buf = tx_buf;
1317 t.rx_buf = rx_buf;
1318 spi_message_init(&m);
1319 spi_message_add_tail(&t, &m);
1320
1321 tx_buf[0] = psoc_channel_switch->cmd;
1322 tx_buf[1] = psoc_channel_switch->dev;
1323 tx_buf[2] = (psoc_channel_switch->delay & 0xff000000) >> 24;
1324 tx_buf[3] = (psoc_channel_switch->delay & 0xff0000) >> 16;
1325 tx_buf[4] = (psoc_channel_switch->delay & 0xff00) >> 8;
1326 tx_buf[5] = psoc_channel_switch->delay & 0xff;
1327
1328 t.len = sizeof(tx_buf);
1329 t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD;
1330
1331 rc = spi_sync(epm_adc->epm_spi_client, &m);
1332 if (rc)
1333 return rc;
1334
1335 rc = spi_sync(epm_adc->epm_spi_client, &m);
1336 if (rc)
1337 return rc;
1338
1339 psoc_channel_switch->cmd = rx_buf[0];
1340 psoc_channel_switch->dev = rx_buf[1];
1341 psoc_channel_switch->delay = rx_buf[2] << 24 |
1342 rx_buf[3] << 16 |
1343 rx_buf[4] << 8 | rx_buf[5];
1344
1345 return rc;
1346}
1347
1348static int epm_psoc_clear_buffer(struct epm_adc_drv *epm_adc)
1349{
1350 struct spi_message m;
1351 struct spi_transfer t;
1352 char tx_buf[3], rx_buf[3];
1353 int rc = 0;
1354
1355 spi_setup(epm_adc->epm_spi_client);
1356
1357 memset(&t, 0, sizeof t);
1358 memset(tx_buf, 0, sizeof tx_buf);
1359 memset(rx_buf, 0, sizeof tx_buf);
1360 t.tx_buf = tx_buf;
1361 t.rx_buf = rx_buf;
1362 spi_message_init(&m);
1363 spi_message_add_tail(&t, &m);
1364
1365 tx_buf[0] = EPM_PSOC_CLEAR_BUFFER_CMD;
1366
1367 t.len = sizeof(tx_buf);
1368 t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD;
1369
1370 rc = spi_sync(epm_adc->epm_spi_client, &m);
1371 if (rc)
1372 return rc;
1373
1374 rc = spi_sync(epm_adc->epm_spi_client, &m);
1375 if (rc)
1376 return rc;
1377
Siddartha Mohanadoss3ff20632013-05-17 10:53:03 -07001378 rc = rx_buf[1];
Siddartha Mohanadossefddea42012-09-04 08:23:43 -07001379
1380 return rc;
1381}
1382
Siddartha Mohanadossd34827d2013-06-12 09:57:30 -07001383static int epm_psoc_get_gpio_buffer_data(struct epm_adc_drv *epm_adc,
1384 struct epm_get_gpio_buffer_resp *gpio_resp_pkt)
1385{
1386 struct spi_message m;
1387 struct spi_transfer t;
1388 char tx_buf[7], rx_buf[7];
1389 int rc = 0;
1390
1391 spi_setup(epm_adc->epm_spi_client);
1392
1393 memset(&t, 0, sizeof t);
1394 memset(tx_buf, 0, sizeof tx_buf);
1395 memset(rx_buf, 0, sizeof tx_buf);
1396 t.tx_buf = tx_buf;
1397 t.rx_buf = rx_buf;
1398 spi_message_init(&m);
1399 spi_message_add_tail(&t, &m);
1400
1401 tx_buf[0] = EPM_PSOC_GET_GPIO_BUFFER_CMD;
1402
1403 t.len = sizeof(tx_buf);
1404 t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD;
1405
1406 rc = spi_sync(epm_adc->epm_spi_client, &m);
1407 if (rc)
1408 return rc;
1409
1410 rc = spi_sync(epm_adc->epm_spi_client, &m);
1411 if (rc)
1412 return rc;
1413
1414 gpio_resp_pkt->cmd = rx_buf[0];
1415 gpio_resp_pkt->status = rx_buf[1];
1416 gpio_resp_pkt->bitmask_monitor_pin = rx_buf[2];
1417 gpio_resp_pkt->timestamp = rx_buf[3] << 24 | rx_buf[4] << 16 |
1418 rx_buf[5] << 8 | tx_buf[6];
1419
1420 return rc;
1421}
1422
1423static int epm_psoc_gpio_buffer_request_configure(struct epm_adc_drv *epm_adc,
1424 struct epm_gpio_buffer_request *gpio_request)
1425{
1426 struct spi_message m;
1427 struct spi_transfer t;
1428 char tx_buf[2], rx_buf[2];
1429 int rc = 0;
1430
1431 spi_setup(epm_adc->epm_spi_client);
1432
1433 memset(&t, 0, sizeof t);
1434 memset(tx_buf, 0, sizeof tx_buf);
1435 memset(rx_buf, 0, sizeof tx_buf);
1436 t.tx_buf = tx_buf;
1437 t.rx_buf = rx_buf;
1438 spi_message_init(&m);
1439 spi_message_add_tail(&t, &m);
1440
1441 tx_buf[0] = EPM_PSOC_GPIO_BUFFER_REQUEST_CMD;
1442 tx_buf[1] = gpio_request->bitmask_monitor_pin;
1443
1444 t.len = sizeof(tx_buf);
1445 t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD;
1446
1447 rc = spi_sync(epm_adc->epm_spi_client, &m);
1448 if (rc)
1449 return rc;
1450
1451 rc = spi_sync(epm_adc->epm_spi_client, &m);
1452 if (rc)
1453 return rc;
1454
1455 gpio_request->cmd = rx_buf[0];
1456 gpio_request->status = rx_buf[1];
1457
1458 return rc;
1459}
1460
Siddartha Mohanadosseb90c922012-03-27 22:58:06 -07001461static long epm_adc_ioctl(struct file *file, unsigned int cmd,
1462 unsigned long arg)
1463{
1464 struct epm_adc_drv *epm_adc = epm_adc_drv;
1465
1466 switch (cmd) {
1467 case EPM_ADC_REQUEST:
1468 {
1469 struct epm_chan_request conv;
1470 int rc;
1471
1472 if (copy_from_user(&conv, (void __user *)arg,
1473 sizeof(struct epm_chan_request)))
1474 return -EFAULT;
1475
1476 rc = epm_adc_blocking_conversion(epm_adc, &conv);
1477 if (rc) {
1478 pr_err("Failed EPM conversion:%d\n", rc);
1479 return rc;
1480 }
1481
1482 if (copy_to_user((void __user *)arg, &conv,
1483 sizeof(struct epm_chan_request)))
1484 return -EFAULT;
1485 break;
1486 }
1487 case EPM_ADC_INIT:
1488 {
1489 uint32_t result;
1490 if (!epm_adc_expander_register) {
1491 result = epm_adc_i2c_expander_register();
1492 if (result) {
1493 pr_err("Failed i2c register:%d\n",
1494 result);
1495 return result;
1496 }
1497 epm_adc_expander_register = true;
1498 }
1499
Siddartha Mohanadossefddea42012-09-04 08:23:43 -07001500 result = epm_adc_hw_init(epm_adc);
Siddartha Mohanadosseb90c922012-03-27 22:58:06 -07001501
1502 if (copy_to_user((void __user *)arg, &result,
1503 sizeof(uint32_t)))
1504 return -EFAULT;
1505 break;
1506 }
1507 case EPM_ADC_DEINIT:
1508 {
1509 uint32_t result;
Siddartha Mohanadossefddea42012-09-04 08:23:43 -07001510 result = epm_adc_hw_deinit(epm_adc);
Siddartha Mohanadosseb90c922012-03-27 22:58:06 -07001511
1512 if (copy_to_user((void __user *)arg, &result,
1513 sizeof(uint32_t)))
1514 return -EFAULT;
1515 break;
1516 }
Siddartha Mohanadoss3efc4d82013-06-11 15:34:52 -07001517 case EPM_MARKER1_REQUEST:
1518 {
1519 struct epm_marker_level marker_init;
1520 uint32_t result;
1521
1522 if (copy_from_user(&marker_init, (void __user *)arg,
1523 sizeof(struct epm_marker_level)))
1524 return -EFAULT;
1525
1526 result = epm_set_marker1(&marker_init);
1527
1528 if (copy_to_user((void __user *)arg, &result,
1529 sizeof(uint32_t)))
1530 return -EFAULT;
1531 break;
1532 }
1533 case EPM_MARKER2_REQUEST:
1534 {
1535 struct epm_marker_level marker_init;
1536 uint32_t result;
1537
1538 if (copy_from_user(&marker_init, (void __user *)arg,
1539 sizeof(struct epm_marker_level)))
1540 return -EFAULT;
1541
1542 result = epm_set_marker2(&marker_init);
1543
1544 if (copy_to_user((void __user *)arg, &result,
1545 sizeof(uint32_t)))
1546 return -EFAULT;
1547 break;
1548 }
1549 case EPM_MARKER1_RELEASE:
1550 {
1551 uint32_t result;
1552 result = epm_marker1_release();
1553
1554 if (copy_to_user((void __user *)arg, &result,
1555 sizeof(uint32_t)))
1556 return -EFAULT;
1557 break;
1558 }
1559 case EPM_MARKER2_RELEASE:
1560 {
1561 uint32_t result;
1562 result = epm_marker2_release();
1563
1564 if (copy_to_user((void __user *)arg, &result,
1565 sizeof(uint32_t)))
1566 return -EFAULT;
1567 break;
1568 }
Siddartha Mohanadossefddea42012-09-04 08:23:43 -07001569 case EPM_PSOC_ADC_INIT:
1570 {
1571 struct epm_psoc_init_resp psoc_init;
1572 int rc;
1573
1574 if (copy_from_user(&psoc_init, (void __user *)arg,
1575 sizeof(struct epm_psoc_init_resp)))
1576 return -EFAULT;
1577
1578 psoc_init.cmd = EPM_PSOC_INIT_CMD;
1579 rc = epm_psoc_init(epm_adc, &psoc_init);
1580 if (rc) {
1581 pr_err("PSOC initialization failed\n");
1582 return -EINVAL;
1583 }
1584
Siddartha Mohanadoss97242fe2013-06-11 16:05:16 -07001585 if (!rc) {
1586 rc = epm_adc_psoc_gpio_init(true);
1587 if (rc) {
1588 pr_err("GPIO init failed\n");
1589 return -EINVAL;
1590 }
1591 }
1592
Siddartha Mohanadossefddea42012-09-04 08:23:43 -07001593 if (copy_to_user((void __user *)arg, &psoc_init,
1594 sizeof(struct epm_psoc_init_resp)))
1595 return -EFAULT;
1596 break;
1597 }
Siddartha Mohanadoss97242fe2013-06-11 16:05:16 -07001598 case EPM_PSOC_ADC_DEINIT:
1599 {
1600 uint32_t result;
1601 result = epm_adc_psoc_gpio_init(false);
1602
1603 if (copy_to_user((void __user *)arg, &result,
1604 sizeof(uint32_t)))
1605 return -EFAULT;
1606 break;
1607 }
Siddartha Mohanadossefddea42012-09-04 08:23:43 -07001608 case EPM_PSOC_ADC_CHANNEL_ENABLE:
1609 case EPM_PSOC_ADC_CHANNEL_DISABLE:
1610 {
1611 struct epm_psoc_channel_configure psoc_chan_configure;
1612 int rc;
1613
1614 if (copy_from_user(&psoc_chan_configure,
1615 (void __user *)arg,
1616 sizeof(struct epm_psoc_channel_configure)))
1617 return -EFAULT;
1618
1619 psoc_chan_configure.cmd =
1620 EPM_PSOC_CHANNEL_ENABLE_DISABLE_CMD;
1621 rc = epm_psoc_channel_configure(epm_adc,
1622 &psoc_chan_configure);
1623 if (rc) {
1624 pr_err("PSOC channel configure failed\n");
1625 return -EINVAL;
1626 }
1627
1628 if (copy_to_user((void __user *)arg,
1629 &psoc_chan_configure,
1630 sizeof(struct epm_psoc_channel_configure)))
1631 return -EFAULT;
1632 break;
1633 }
1634 case EPM_PSOC_ADC_SET_AVERAGING:
1635 {
1636 struct epm_psoc_set_avg psoc_set_avg;
1637 int rc;
1638
1639 if (copy_from_user(&psoc_set_avg, (void __user *)arg,
1640 sizeof(struct epm_psoc_set_avg)))
1641 return -EFAULT;
1642
1643 psoc_set_avg.cmd = EPM_PSOC_SET_AVERAGING_CMD;
1644 rc = epm_psoc_set_averaging(epm_adc, &psoc_set_avg);
1645 if (rc) {
1646 pr_err("PSOC averaging failed\n");
1647 return -EINVAL;
1648 }
1649
1650 if (copy_to_user((void __user *)arg, &psoc_set_avg,
1651 sizeof(struct epm_psoc_set_avg)))
1652 return -EFAULT;
1653 break;
1654 }
1655 case EPM_PSOC_ADC_GET_LAST_MEASUREMENT:
1656 {
1657 struct epm_psoc_get_data psoc_get_data;
1658 int rc;
1659
1660 if (copy_from_user(&psoc_get_data,
1661 (void __user *)arg,
1662 sizeof(struct epm_psoc_get_data)))
1663 return -EFAULT;
1664
1665 psoc_get_data.cmd = EPM_PSOC_GET_LAST_MEASUREMENT_CMD;
1666 rc = epm_psoc_get_data(epm_adc, &psoc_get_data);
1667 if (rc) {
1668 pr_err("PSOC last measured data failed\n");
1669 return -EINVAL;
1670 }
1671
Siddartha Mohanadoss53c6ebe2012-12-06 14:17:30 -08001672 psoc_get_data.reading_value = epm_psoc_scale_result(
Siddartha Mohanadoss899173a2013-01-29 19:18:50 -08001673 psoc_get_data.reading_raw,
Siddartha Mohanadoss53c6ebe2012-12-06 14:17:30 -08001674 psoc_get_data.chan_num);
1675
Siddartha Mohanadossefddea42012-09-04 08:23:43 -07001676 if (copy_to_user((void __user *)arg, &psoc_get_data,
1677 sizeof(struct epm_psoc_get_data)))
1678 return -EFAULT;
1679 break;
1680 }
1681 case EPM_PSOC_ADC_GET_BUFFERED_DATA:
1682 {
1683 struct epm_psoc_get_buffered_data psoc_get_data;
1684 int rc;
1685
1686 if (copy_from_user(&psoc_get_data,
1687 (void __user *)arg,
1688 sizeof(struct epm_psoc_get_buffered_data)))
1689 return -EFAULT;
1690
1691 psoc_get_data.cmd = EPM_PSOC_GET_BUFFERED_DATA_CMD;
1692 rc = epm_psoc_get_buffered_data(epm_adc,
1693 &psoc_get_data);
1694 if (rc) {
1695 pr_err("PSOC buffered measurement failed\n");
1696 return -EINVAL;
1697 }
1698
1699 if (copy_to_user((void __user *)arg, &psoc_get_data,
1700 sizeof(struct epm_psoc_get_buffered_data)))
1701 return -EFAULT;
1702 break;
1703 }
1704 case EPM_PSOC_ADC_GET_SYSTEM_TIMESTAMP:
Siddartha Mohanadoss278815e2013-04-22 11:18:17 -07001705 {
1706 struct epm_psoc_system_time_stamp psoc_timestamp;
1707 int rc;
1708
1709 if (copy_from_user(&psoc_timestamp,
1710 (void __user *)arg,
1711 sizeof(struct epm_psoc_system_time_stamp)))
1712 return -EFAULT;
1713
1714 rc = epm_psoc_get_timestamp(epm_adc, &psoc_timestamp);
1715 if (rc) {
1716 pr_err("PSOC get timestamp failed\n");
1717 return -EINVAL;
1718 }
1719
1720 if (copy_to_user((void __user *)arg, &psoc_timestamp,
1721 sizeof(struct epm_psoc_system_time_stamp)))
1722 return -EFAULT;
1723 break;
1724 }
Siddartha Mohanadossefddea42012-09-04 08:23:43 -07001725 case EPM_PSOC_ADC_SET_SYSTEM_TIMESTAMP:
1726 {
1727 struct epm_psoc_system_time_stamp psoc_timestamp;
1728 int rc;
1729
1730 if (copy_from_user(&psoc_timestamp,
1731 (void __user *)arg,
1732 sizeof(struct epm_psoc_system_time_stamp)))
1733 return -EFAULT;
1734
Siddartha Mohanadoss278815e2013-04-22 11:18:17 -07001735 rc = epm_psoc_set_timestamp(epm_adc, &psoc_timestamp);
Siddartha Mohanadossefddea42012-09-04 08:23:43 -07001736 if (rc) {
Siddartha Mohanadoss278815e2013-04-22 11:18:17 -07001737 pr_err("PSOC set timestamp failed\n");
Siddartha Mohanadossefddea42012-09-04 08:23:43 -07001738 return -EINVAL;
1739 }
1740
1741 if (copy_to_user((void __user *)arg, &psoc_timestamp,
1742 sizeof(struct epm_psoc_system_time_stamp)))
1743 return -EFAULT;
1744 break;
1745 }
1746 case EPM_PSOC_ADC_GET_AVERAGE_DATA:
1747 {
1748 struct epm_psoc_get_avg_buffered_switch_data
1749 psoc_get_data;
1750 int rc;
1751
1752 if (copy_from_user(&psoc_get_data,
1753 (void __user *)arg,
1754 sizeof(struct
1755 epm_psoc_get_avg_buffered_switch_data)))
1756 return -EFAULT;
1757
1758 psoc_get_data.cmd = EPM_PSOC_GET_AVERAGED_DATA_CMD;
1759 rc = epm_psoc_get_avg_buffered_switch_data(epm_adc,
1760 &psoc_get_data);
1761 if (rc) {
1762 pr_err("Get averaged buffered data failed\n");
1763 return -EINVAL;
1764 }
1765
1766 if (copy_to_user((void __user *)arg, &psoc_get_data,
1767 sizeof(struct
1768 epm_psoc_get_avg_buffered_switch_data)))
1769 return -EFAULT;
1770 break;
1771 }
1772 case EPM_PSOC_SET_CHANNEL_SWITCH:
1773 {
1774 struct epm_psoc_set_channel_switch psoc_channel_switch;
1775 int rc;
1776
1777 if (copy_from_user(&psoc_channel_switch,
1778 (void __user *)arg,
1779 sizeof(struct epm_psoc_set_channel_switch)))
1780 return -EFAULT;
1781
1782 rc = epm_psoc_set_channel_switch(epm_adc,
1783 &psoc_channel_switch);
1784 if (rc) {
1785 pr_err("PSOC channel switch failed\n");
1786 return -EINVAL;
1787 }
1788
1789 if (copy_to_user((void __user *)arg,
1790 &psoc_channel_switch,
1791 sizeof(struct epm_psoc_set_channel_switch)))
1792 return -EFAULT;
1793 break;
1794 }
1795 case EPM_PSOC_CLEAR_BUFFER:
1796 {
1797 int rc;
1798 rc = epm_psoc_clear_buffer(epm_adc);
1799 if (rc) {
1800 pr_err("PSOC clear buffer failed\n");
1801 return -EINVAL;
1802 }
1803
1804 if (copy_to_user((void __user *)arg, &rc,
1805 sizeof(uint32_t)))
1806 return -EFAULT;
1807 break;
1808 }
1809 case EPM_PSOC_ADC_SET_VADC_REFERENCE:
1810 {
1811 struct epm_psoc_set_vadc psoc_set_vadc;
1812 int rc;
1813
1814 if (copy_from_user(&psoc_set_vadc,
1815 (void __user *)arg,
1816 sizeof(struct epm_psoc_set_vadc)))
1817 return -EFAULT;
1818
1819 rc = epm_psoc_set_vadc(epm_adc, &psoc_set_vadc);
1820 if (rc) {
1821 pr_err("PSOC set VADC failed\n");
1822 return -EINVAL;
1823 }
1824
1825 if (copy_to_user((void __user *)arg, &psoc_set_vadc,
1826 sizeof(struct epm_psoc_set_vadc)))
1827 return -EFAULT;
1828 break;
1829 }
Siddartha Mohanadossd34827d2013-06-12 09:57:30 -07001830 case EPM_PSOC_GPIO_BUFFER_REQUEST:
1831 {
1832 struct epm_gpio_buffer_request gpio_request;
1833 int rc;
1834
1835 if (copy_from_user(&gpio_request,
1836 (void __user *)arg,
1837 sizeof(struct epm_gpio_buffer_request)))
1838 return -EFAULT;
1839
1840 rc = epm_psoc_gpio_buffer_request_configure(epm_adc,
1841 &gpio_request);
1842 if (rc) {
1843 pr_err("PSOC buffer request failed\n");
1844 return -EINVAL;
1845 }
1846
1847 if (copy_to_user((void __user *)arg, &gpio_request,
1848 sizeof(struct epm_gpio_buffer_request)))
1849 return -EFAULT;
1850 break;
1851 }
1852 case EPM_PSOC_GET_GPIO_BUFFER_DATA:
1853 {
1854 struct epm_get_gpio_buffer_resp gpio_resp_pkt;
1855 int rc;
1856
1857 if (copy_from_user(&gpio_resp_pkt,
1858 (void __user *)arg,
1859 sizeof(struct epm_get_gpio_buffer_resp)))
1860 return -EFAULT;
1861
1862 rc = epm_psoc_get_gpio_buffer_data(epm_adc,
1863 &gpio_resp_pkt);
1864 if (rc) {
1865 pr_err("PSOC get buffer data failed\n");
1866 return -EINVAL;
1867 }
1868
1869 if (copy_to_user((void __user *)arg, &gpio_resp_pkt,
1870 sizeof(struct epm_get_gpio_buffer_resp)))
1871 return -EFAULT;
1872 break;
1873 }
Siddartha Mohanadoss46aff912013-07-10 18:53:48 -07001874 case EPM_PSOC_PAUSE_CONVERSION_REQUEST:
1875 {
1876 uint32_t result;
1877 result = epm_psoc_pause_conversion(epm_adc);
1878
1879 if (copy_to_user((void __user *)arg, &result,
1880 sizeof(uint32_t)))
1881 return -EFAULT;
1882 break;
1883 }
1884 case EPM_PSOC_UNPAUSE_CONVERSION_REQUEST:
1885 {
1886 uint32_t result;
1887 result = epm_psoc_unpause_conversion(epm_adc);
1888
1889 if (copy_to_user((void __user *)arg, &result,
1890 sizeof(uint32_t)))
1891 return -EFAULT;
1892 break;
1893 }
Siddartha Mohanadosseb90c922012-03-27 22:58:06 -07001894 default:
1895 return -EINVAL;
1896 }
1897
1898 return 0;
1899}
1900
1901const struct file_operations epm_adc_fops = {
1902 .unlocked_ioctl = epm_adc_ioctl,
1903};
1904
Siddartha Mohanadossefddea42012-09-04 08:23:43 -07001905static ssize_t epm_adc_psoc_show_in(struct device *dev,
1906 struct device_attribute *devattr, char *buf)
1907{
1908 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1909 struct epm_adc_drv *epm_adc = epm_adc_drv;
1910 struct epm_psoc_init_resp init_resp;
1911 struct epm_psoc_channel_configure psoc_chan_configure;
1912 struct epm_psoc_get_data psoc_get_meas;
Siddartha Mohanadossefddea42012-09-04 08:23:43 -07001913 int rc = 0;
1914
1915 rc = epm_adc_psoc_gpio_init(true);
1916 if (rc) {
1917 pr_err("GPIO init failed\n");
1918 return 0;
1919 }
1920 usleep_range(EPM_GLOBAL_ENABLE_MIN_DELAY,
1921 EPM_GLOBAL_ENABLE_MAX_DELAY);
1922
1923 init_resp.cmd = EPM_PSOC_INIT_CMD;
1924 rc = epm_psoc_init(epm_adc, &init_resp);
1925 if (rc) {
Siddartha Mohanadoss6e77b772012-09-19 21:58:13 -07001926 pr_err("PSOC init failed %d\n", rc);
Siddartha Mohanadossefddea42012-09-04 08:23:43 -07001927 return 0;
1928 }
1929
Siddartha Mohanadoss6e77b772012-09-19 21:58:13 -07001930
Siddartha Mohanadossefddea42012-09-04 08:23:43 -07001931 psoc_chan_configure.channel_num = (1 << attr->index);
1932 psoc_chan_configure.cmd = EPM_PSOC_CHANNEL_ENABLE_DISABLE_CMD;
1933 rc = epm_psoc_channel_configure(epm_adc, &psoc_chan_configure);
1934 if (rc) {
Siddartha Mohanadoss6e77b772012-09-19 21:58:13 -07001935 pr_err("PSOC channel configure failed\n");
Siddartha Mohanadossefddea42012-09-04 08:23:43 -07001936 return 0;
1937 }
1938
1939 usleep_range(EPM_GLOBAL_ENABLE_MIN_DELAY,
1940 EPM_GLOBAL_ENABLE_MAX_DELAY);
1941
1942 psoc_get_meas.cmd = EPM_PSOC_GET_LAST_MEASUREMENT_CMD;
1943 psoc_get_meas.dev_num = 0;
1944 psoc_get_meas.chan_num = attr->index;
1945 rc = epm_psoc_get_data(epm_adc, &psoc_get_meas);
1946 if (rc) {
Siddartha Mohanadoss6e77b772012-09-19 21:58:13 -07001947 pr_err("PSOC get data failed\n");
Siddartha Mohanadossefddea42012-09-04 08:23:43 -07001948 return 0;
1949 }
1950
Siddartha Mohanadoss6e77b772012-09-19 21:58:13 -07001951 psoc_get_meas.reading_value = epm_psoc_scale_result(
1952 psoc_get_meas.reading_value,
1953 attr->index);
Siddartha Mohanadossefddea42012-09-04 08:23:43 -07001954
1955 rc = epm_adc_psoc_gpio_init(false);
1956 if (rc) {
1957 pr_err("GPIO de-init failed\n");
1958 return 0;
1959 }
1960
1961 return snprintf(buf, 16, "Result: %d\n", psoc_get_meas.reading_value);
1962}
1963
1964static struct sensor_device_attribute epm_adc_psoc_in_attrs[] = {
Siddartha Mohanadoss37e1c372012-12-06 14:19:11 -08001965 SENSOR_ATTR(psoc0_chan0, S_IRUGO, epm_adc_psoc_show_in, NULL, 0),
1966 SENSOR_ATTR(psoc0_chan1, S_IRUGO, epm_adc_psoc_show_in, NULL, 1),
1967 SENSOR_ATTR(psoc0_chan2, S_IRUGO, epm_adc_psoc_show_in, NULL, 2),
1968 SENSOR_ATTR(psoc0_chan3, S_IRUGO, epm_adc_psoc_show_in, NULL, 3),
1969 SENSOR_ATTR(psoc0_chan4, S_IRUGO, epm_adc_psoc_show_in, NULL, 4),
1970 SENSOR_ATTR(psoc0_chan5, S_IRUGO, epm_adc_psoc_show_in, NULL, 5),
1971 SENSOR_ATTR(psoc0_chan6, S_IRUGO, epm_adc_psoc_show_in, NULL, 6),
1972 SENSOR_ATTR(psoc0_chan7, S_IRUGO, epm_adc_psoc_show_in, NULL, 7),
1973 SENSOR_ATTR(psoc0_chan8, S_IRUGO, epm_adc_psoc_show_in, NULL, 8),
1974 SENSOR_ATTR(psoc0_chan9, S_IRUGO, epm_adc_psoc_show_in, NULL, 9),
1975 SENSOR_ATTR(psoc0_chan10, S_IRUGO, epm_adc_psoc_show_in, NULL, 10),
1976 SENSOR_ATTR(psoc0_chan11, S_IRUGO, epm_adc_psoc_show_in, NULL, 11),
1977 SENSOR_ATTR(psoc0_chan12, S_IRUGO, epm_adc_psoc_show_in, NULL, 12),
1978 SENSOR_ATTR(psoc0_chan13, S_IRUGO, epm_adc_psoc_show_in, NULL, 13),
1979 SENSOR_ATTR(psoc0_chan14, S_IRUGO, epm_adc_psoc_show_in, NULL, 14),
1980 SENSOR_ATTR(psoc0_chan15, S_IRUGO, epm_adc_psoc_show_in, NULL, 15),
1981 SENSOR_ATTR(psoc0_chan16, S_IRUGO, epm_adc_psoc_show_in, NULL, 16),
1982 SENSOR_ATTR(psoc0_chan17, S_IRUGO, epm_adc_psoc_show_in, NULL, 17),
1983 SENSOR_ATTR(psoc0_chan18, S_IRUGO, epm_adc_psoc_show_in, NULL, 18),
1984 SENSOR_ATTR(psoc0_chan19, S_IRUGO, epm_adc_psoc_show_in, NULL, 19),
1985 SENSOR_ATTR(psoc0_chan20, S_IRUGO, epm_adc_psoc_show_in, NULL, 20),
1986 SENSOR_ATTR(psoc0_chan21, S_IRUGO, epm_adc_psoc_show_in, NULL, 21),
1987 SENSOR_ATTR(psoc0_chan22, S_IRUGO, epm_adc_psoc_show_in, NULL, 22),
1988 SENSOR_ATTR(psoc0_chan23, S_IRUGO, epm_adc_psoc_show_in, NULL, 23),
1989 SENSOR_ATTR(psoc0_chan24, S_IRUGO, epm_adc_psoc_show_in, NULL, 24),
1990 SENSOR_ATTR(psoc0_chan25, S_IRUGO, epm_adc_psoc_show_in, NULL, 25),
1991 SENSOR_ATTR(psoc0_chan26, S_IRUGO, epm_adc_psoc_show_in, NULL, 26),
1992 SENSOR_ATTR(psoc0_chan27, S_IRUGO, epm_adc_psoc_show_in, NULL, 27),
1993 SENSOR_ATTR(psoc0_chan28, S_IRUGO, epm_adc_psoc_show_in, NULL, 28),
1994 SENSOR_ATTR(psoc0_chan29, S_IRUGO, epm_adc_psoc_show_in, NULL, 29),
1995 SENSOR_ATTR(psoc0_chan30, S_IRUGO, epm_adc_psoc_show_in, NULL, 30),
1996 SENSOR_ATTR(psoc0_chan31, S_IRUGO, epm_adc_psoc_show_in, NULL, 31),
Siddartha Mohanadossefddea42012-09-04 08:23:43 -07001997};
1998
1999static int __devinit epm_adc_psoc_init_hwmon(struct spi_device *spi,
2000 struct epm_adc_drv *epm_adc)
2001{
Siddartha Mohanadoss6e77b772012-09-19 21:58:13 -07002002 int i, rc, num_chans = 31;
Siddartha Mohanadossefddea42012-09-04 08:23:43 -07002003
2004 for (i = 0; i < num_chans; i++) {
2005 rc = device_create_file(&spi->dev,
2006 &epm_adc_psoc_in_attrs[i].dev_attr);
2007 if (rc) {
2008 dev_err(&spi->dev, "device_create_file failed\n");
2009 return rc;
2010 }
2011 }
2012
2013 return 0;
2014}
2015
2016static int get_device_tree_data(struct spi_device *spi)
2017{
2018 const struct device_node *node = spi->dev.of_node;
2019 struct epm_adc_drv *epm_adc;
Siddartha Mohanadoss6e77b772012-09-19 21:58:13 -07002020 u32 *epm_ch_gain, *epm_ch_rsense;
2021 u32 rc = 0, epm_num_channels, i, channel_mask;
Siddartha Mohanadossefddea42012-09-04 08:23:43 -07002022
2023 if (!node)
2024 return -EINVAL;
2025
2026 rc = of_property_read_u32(node,
2027 "qcom,channels", &epm_num_channels);
2028 if (rc) {
2029 dev_err(&spi->dev, "missing channel numbers\n");
2030 return -ENODEV;
2031 }
2032
2033 epm_ch_gain = devm_kzalloc(&spi->dev,
Siddartha Mohanadoss6e77b772012-09-19 21:58:13 -07002034 epm_num_channels * sizeof(u32), GFP_KERNEL);
Siddartha Mohanadossefddea42012-09-04 08:23:43 -07002035 if (!epm_ch_gain) {
2036 dev_err(&spi->dev, "cannot allocate gain\n");
2037 return -ENOMEM;
2038 }
2039
2040 epm_ch_rsense = devm_kzalloc(&spi->dev,
Siddartha Mohanadoss6e77b772012-09-19 21:58:13 -07002041 epm_num_channels * sizeof(u32), GFP_KERNEL);
Siddartha Mohanadossefddea42012-09-04 08:23:43 -07002042 if (!epm_ch_rsense) {
2043 dev_err(&spi->dev, "cannot allocate rsense\n");
2044 return -ENOMEM;
2045 }
2046
2047 rc = of_property_read_u32_array(node,
2048 "qcom,gain", epm_ch_gain, epm_num_channels);
2049 if (rc) {
2050 dev_err(&spi->dev, "invalid gain property:%d\n", rc);
2051 return rc;
2052 }
2053
2054 rc = of_property_read_u32_array(node,
2055 "qcom,rsense", epm_ch_rsense, epm_num_channels);
2056 if (rc) {
2057 dev_err(&spi->dev, "invalid rsense property:%d\n", rc);
2058 return rc;
2059 }
2060
Siddartha Mohanadoss6e77b772012-09-19 21:58:13 -07002061 rc = of_property_read_u32(node,
2062 "qcom,channel-type", &channel_mask);
2063 if (rc) {
2064 dev_err(&spi->dev, "missing channel mask\n");
2065 return -ENODEV;
2066 }
2067
Siddartha Mohanadossefddea42012-09-04 08:23:43 -07002068 epm_adc = devm_kzalloc(&spi->dev,
2069 sizeof(struct epm_adc_drv) +
2070 (epm_num_channels *
2071 sizeof(struct epm_chan_properties)),
2072 GFP_KERNEL);
2073 if (!epm_adc) {
2074 dev_err(&spi->dev, "Unable to allocate memory\n");
2075 return -ENOMEM;
2076 }
2077
2078 for (i = 0; i < epm_num_channels; i++) {
2079 epm_adc->epm_psoc_ch_prop[i].resistorvalue =
2080 epm_ch_rsense[i];
2081 epm_adc->epm_psoc_ch_prop[i].gain =
2082 epm_ch_gain[i];
2083 }
2084
Siddartha Mohanadoss6e77b772012-09-19 21:58:13 -07002085 epm_adc->channel_mask = channel_mask;
Siddartha Mohanadossefddea42012-09-04 08:23:43 -07002086 epm_adc_drv = epm_adc;
2087
2088 return 0;
2089}
2090
2091static int __devinit epm_adc_psoc_spi_probe(struct spi_device *spi)
2092{
Siddartha Mohanadoss6e77b772012-09-19 21:58:13 -07002093
Siddartha Mohanadossefddea42012-09-04 08:23:43 -07002094 struct epm_adc_drv *epm_adc;
2095 struct device_node *node = spi->dev.of_node;
2096 int rc = 0;
2097
Siddartha Mohanadoss6e77b772012-09-19 21:58:13 -07002098 if (node) {
Siddartha Mohanadossefddea42012-09-04 08:23:43 -07002099 rc = get_device_tree_data(spi);
Siddartha Mohanadoss6e77b772012-09-19 21:58:13 -07002100 if (rc)
2101 return rc;
2102 } else {
Siddartha Mohanadossde90ed42012-09-18 16:57:50 -07002103 epm_adc = epm_adc_drv;
2104 epm_adc_drv->epm_spi_client = spi;
2105 epm_adc_drv->epm_spi_client->bits_per_word =
2106 EPM_ADC_ADS_SPI_BITS_PER_WORD;
2107 return rc;
2108 }
Siddartha Mohanadossefddea42012-09-04 08:23:43 -07002109
2110 epm_adc = epm_adc_drv;
2111 epm_adc->misc.name = EPM_ADC_DRIVER_NAME;
2112 epm_adc->misc.minor = MISC_DYNAMIC_MINOR;
Siddartha Mohanadoss5e8df032012-11-12 17:54:03 -08002113
2114 if (node) {
2115 epm_adc->misc.fops = &epm_adc_fops;
2116 if (misc_register(&epm_adc->misc)) {
2117 pr_err("Unable to register misc device!\n");
2118 return -EFAULT;
2119 }
2120 }
2121
Siddartha Mohanadossefddea42012-09-04 08:23:43 -07002122 epm_adc_drv->epm_spi_client = spi;
2123 epm_adc_drv->epm_spi_client->bits_per_word =
2124 EPM_ADC_ADS_SPI_BITS_PER_WORD;
2125 rc = epm_adc_psoc_init_hwmon(spi, epm_adc);
2126 if (rc) {
2127 dev_err(&spi->dev, "msm_adc_dev_init failed\n");
2128 return rc;
2129 }
2130
2131 epm_adc->hwmon = hwmon_device_register(&spi->dev);
2132 if (IS_ERR(epm_adc->hwmon)) {
2133 dev_err(&spi->dev, "hwmon_device_register failed\n");
2134 return rc;
2135 }
2136
2137 mutex_init(&epm_adc->conv_lock);
Siddartha Mohanadossefddea42012-09-04 08:23:43 -07002138 return rc;
2139}
2140
2141static int __devexit epm_adc_psoc_spi_remove(struct spi_device *spi)
2142{
2143 epm_adc_drv->epm_spi_client = NULL;
2144 return 0;
2145}
2146
2147static const struct of_device_id epm_adc_psoc_match_table[] = {
Siddartha Mohanadoss6eadd582012-10-25 15:29:22 -07002148 { .compatible = "cy,epm-adc-cy8c5568lti-114",
Siddartha Mohanadossefddea42012-09-04 08:23:43 -07002149 },
2150 {}
2151};
2152
2153static struct spi_driver epm_spi_driver = {
2154 .probe = epm_adc_psoc_spi_probe,
2155 .remove = __devexit_p(epm_adc_psoc_spi_remove),
2156 .driver = {
2157 .name = EPM_ADC_DRIVER_NAME,
2158 .of_match_table = epm_adc_psoc_match_table,
2159 },
2160};
2161
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -07002162static ssize_t epm_adc_show_in(struct device *dev,
2163 struct device_attribute *devattr, char *buf)
2164{
2165 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
2166 struct epm_adc_drv *epm_adc = dev_get_drvdata(dev);
2167 struct epm_adc_platform_data *pdata = epm_adc->pdev->dev.platform_data;
2168 struct epm_chan_request conv;
2169 int rc = 0;
2170
2171 conv.device_idx = attr->index / pdata->chan_per_adc;
2172 conv.channel_idx = attr->index % pdata->chan_per_adc;
2173 conv.physical = 0;
Siddartha Mohanadoss6e77b772012-09-19 21:58:13 -07002174
Siddartha Mohanadosseb90c922012-03-27 22:58:06 -07002175 if (!epm_adc_expander_register) {
2176 rc = epm_adc_i2c_expander_register();
2177 if (rc) {
2178 pr_err("I2C expander register failed:%d\n", rc);
2179 return rc;
2180 }
2181 epm_adc_expander_register = true;
2182 }
2183
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -07002184 rc = epm_adc_hw_init(epm_adc);
2185 if (rc) {
2186 pr_err("%s: epm_adc_hw_init() failed, rc = %d",
2187 __func__, rc);
2188 return 0;
2189 }
2190
2191 rc = epm_adc_blocking_conversion(epm_adc, &conv);
2192 if (rc) {
2193 pr_err("%s: epm_adc_blocking_conversion() failed, rc = %d\n",
2194 __func__, rc);
2195 return 0;
2196 }
Siddartha Mohanadoss6e77b772012-09-19 21:58:13 -07002197
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -07002198 rc = epm_adc_hw_deinit(epm_adc);
2199 if (rc) {
2200 pr_err("%s: epm_adc_hw_deinit() failed, rc = %d",
2201 __func__, rc);
2202 return 0;
2203 }
2204
2205 return snprintf(buf, 16, "Result: %d\n", conv.physical);
2206}
2207
Stephen Boydaeaf2ad2012-07-03 14:33:25 -07002208static struct sensor_device_attribute epm_adc_in_attrs[] = {
2209 SENSOR_ATTR(ads0_chan0, S_IRUGO, epm_adc_show_in, NULL, 0),
2210 SENSOR_ATTR(ads0_chan1, S_IRUGO, epm_adc_show_in, NULL, 1),
2211 SENSOR_ATTR(ads0_chan2, S_IRUGO, epm_adc_show_in, NULL, 2),
2212 SENSOR_ATTR(ads0_chan3, S_IRUGO, epm_adc_show_in, NULL, 3),
2213 SENSOR_ATTR(ads0_chan4, S_IRUGO, epm_adc_show_in, NULL, 4),
2214 SENSOR_ATTR(ads0_chan5, S_IRUGO, epm_adc_show_in, NULL, 5),
2215 SENSOR_ATTR(ads0_chan6, S_IRUGO, epm_adc_show_in, NULL, 6),
2216 SENSOR_ATTR(ads0_chan7, S_IRUGO, epm_adc_show_in, NULL, 7),
2217 SENSOR_ATTR(ads0_chan8, S_IRUGO, epm_adc_show_in, NULL, 8),
2218 SENSOR_ATTR(ads0_chan9, S_IRUGO, epm_adc_show_in, NULL, 9),
2219 SENSOR_ATTR(ads0_chan10, S_IRUGO, epm_adc_show_in, NULL, 10),
2220 SENSOR_ATTR(ads0_chan11, S_IRUGO, epm_adc_show_in, NULL, 11),
2221 SENSOR_ATTR(ads0_chan12, S_IRUGO, epm_adc_show_in, NULL, 12),
2222 SENSOR_ATTR(ads0_chan13, S_IRUGO, epm_adc_show_in, NULL, 13),
2223 SENSOR_ATTR(ads0_chan14, S_IRUGO, epm_adc_show_in, NULL, 14),
2224 SENSOR_ATTR(ads0_chan15, S_IRUGO, epm_adc_show_in, NULL, 15),
2225 SENSOR_ATTR(ads1_chan0, S_IRUGO, epm_adc_show_in, NULL, 16),
2226 SENSOR_ATTR(ads1_chan1, S_IRUGO, epm_adc_show_in, NULL, 17),
2227 SENSOR_ATTR(ads1_chan2, S_IRUGO, epm_adc_show_in, NULL, 18),
2228 SENSOR_ATTR(ads1_chan3, S_IRUGO, epm_adc_show_in, NULL, 19),
2229 SENSOR_ATTR(ads1_chan4, S_IRUGO, epm_adc_show_in, NULL, 20),
2230 SENSOR_ATTR(ads1_chan5, S_IRUGO, epm_adc_show_in, NULL, 21),
2231 SENSOR_ATTR(ads1_chan6, S_IRUGO, epm_adc_show_in, NULL, 22),
2232 SENSOR_ATTR(ads1_chan7, S_IRUGO, epm_adc_show_in, NULL, 23),
2233 SENSOR_ATTR(ads1_chan8, S_IRUGO, epm_adc_show_in, NULL, 24),
2234 SENSOR_ATTR(ads1_chan9, S_IRUGO, epm_adc_show_in, NULL, 25),
2235 SENSOR_ATTR(ads1_chan10, S_IRUGO, epm_adc_show_in, NULL, 26),
2236 SENSOR_ATTR(ads1_chan11, S_IRUGO, epm_adc_show_in, NULL, 27),
2237 SENSOR_ATTR(ads1_chan12, S_IRUGO, epm_adc_show_in, NULL, 28),
2238 SENSOR_ATTR(ads1_chan13, S_IRUGO, epm_adc_show_in, NULL, 29),
2239 SENSOR_ATTR(ads1_chan14, S_IRUGO, epm_adc_show_in, NULL, 30),
2240 SENSOR_ATTR(ads1_chan15, S_IRUGO, epm_adc_show_in, NULL, 31),
2241};
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -07002242
2243static int __devinit epm_adc_init_hwmon(struct platform_device *pdev,
2244 struct epm_adc_drv *epm_adc)
2245{
2246 struct epm_adc_platform_data *pdata = pdev->dev.platform_data;
Stephen Boydaeaf2ad2012-07-03 14:33:25 -07002247 int i, rc, num_chans = pdata->num_channels;
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -07002248
Stephen Boydaeaf2ad2012-07-03 14:33:25 -07002249 for (i = 0; i < num_chans; i++) {
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -07002250 rc = device_create_file(&pdev->dev,
Stephen Boydaeaf2ad2012-07-03 14:33:25 -07002251 &epm_adc_in_attrs[i].dev_attr);
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -07002252 if (rc) {
2253 dev_err(&pdev->dev, "device_create_file failed\n");
2254 return rc;
2255 }
2256 }
2257
Stephen Boydaeaf2ad2012-07-03 14:33:25 -07002258 return 0;
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -07002259}
2260
Stephen Boyd58701e82012-04-25 11:48:28 -07002261static int __devinit epm_adc_probe(struct platform_device *pdev)
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -07002262{
2263 struct epm_adc_drv *epm_adc;
2264 struct epm_adc_platform_data *pdata = pdev->dev.platform_data;
2265 int rc = 0;
2266
2267 if (!pdata) {
2268 dev_err(&pdev->dev, "no platform data?\n");
2269 return -EINVAL;
2270 }
2271
2272 epm_adc = kzalloc(sizeof(struct epm_adc_drv), GFP_KERNEL);
2273 if (!epm_adc) {
2274 dev_err(&pdev->dev, "Unable to allocate memory\n");
2275 return -ENOMEM;
2276 }
2277
2278 platform_set_drvdata(pdev, epm_adc);
2279 epm_adc_drv = epm_adc;
2280 epm_adc->pdev = pdev;
2281
Siddartha Mohanadosseb90c922012-03-27 22:58:06 -07002282 epm_adc->misc.name = EPM_ADC_DRIVER_NAME;
2283 epm_adc->misc.minor = MISC_DYNAMIC_MINOR;
2284 epm_adc->misc.fops = &epm_adc_fops;
2285
2286 if (misc_register(&epm_adc->misc)) {
2287 dev_err(&pdev->dev, "Unable to register misc device!\n");
2288 return -EFAULT;
2289 }
2290
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -07002291 rc = epm_adc_init_hwmon(pdev, epm_adc);
2292 if (rc) {
2293 dev_err(&pdev->dev, "msm_adc_dev_init failed\n");
Siddartha Mohanadosseb90c922012-03-27 22:58:06 -07002294 misc_deregister(&epm_adc->misc);
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -07002295 return rc;
2296 }
2297
2298 epm_adc->hwmon = hwmon_device_register(&pdev->dev);
2299 if (IS_ERR(epm_adc->hwmon)) {
2300 dev_err(&pdev->dev, "hwmon_device_register failed\n");
Siddartha Mohanadosseb90c922012-03-27 22:58:06 -07002301 misc_deregister(&epm_adc->misc);
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -07002302 rc = PTR_ERR(epm_adc->hwmon);
2303 return rc;
2304 }
2305
2306 mutex_init(&epm_adc->conv_lock);
2307 epm_i2c_info = &pdata->epm_i2c_board_info;
2308 epm_adc->bus_id = pdata->bus_id;
2309 epm_gpio_expander_base_addr = pdata->gpio_expander_base_addr;
Siddartha Mohanadosseb90c922012-03-27 22:58:06 -07002310 epm_adc_expander_register = false;
Siddartha Mohanadossefddea42012-09-04 08:23:43 -07002311
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -07002312 return rc;
2313}
2314
2315static int __devexit epm_adc_remove(struct platform_device *pdev)
2316{
2317 struct epm_adc_drv *epm_adc = platform_get_drvdata(pdev);
2318 struct epm_adc_platform_data *pdata = pdev->dev.platform_data;
2319 int num_chans = pdata->num_channels;
2320 int i = 0;
2321
Stephen Boydaeaf2ad2012-07-03 14:33:25 -07002322 for (i = 0; i < num_chans; i++)
2323 device_remove_file(&pdev->dev, &epm_adc_in_attrs[i].dev_attr);
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -07002324 hwmon_device_unregister(epm_adc->hwmon);
Siddartha Mohanadosseb90c922012-03-27 22:58:06 -07002325 misc_deregister(&epm_adc->misc);
Siddartha Mohanadoss53c16f92011-10-17 15:57:15 -07002326 epm_adc = NULL;
2327
2328 return 0;
2329}
2330
2331static struct platform_driver epm_adc_driver = {
2332 .probe = epm_adc_probe,
2333 .remove = __devexit_p(epm_adc_remove),
2334 .driver = {
2335 .name = EPM_ADC_DRIVER_NAME,
2336 .owner = THIS_MODULE,
2337 },
2338};
2339
2340static int __init epm_adc_init(void)
2341{
2342 int ret = 0;
2343
2344 ret = platform_driver_register(&epm_adc_driver);
2345 if (ret) {
2346 pr_err("%s: driver register failed, rc=%d\n", __func__, ret);
2347 return ret;
2348 }
2349
2350 ret = spi_register_driver(&epm_spi_driver);
2351 if (ret)
2352 pr_err("%s: spi register failed: rc=%d\n", __func__, ret);
2353
2354 return ret;
2355}
2356
2357static void __exit epm_adc_exit(void)
2358{
2359 spi_unregister_driver(&epm_spi_driver);
2360 platform_driver_unregister(&epm_adc_driver);
2361}
2362
2363module_init(epm_adc_init);
2364module_exit(epm_adc_exit);
2365
2366MODULE_DESCRIPTION("EPM ADC Driver");
2367MODULE_ALIAS("platform:epm_adc");
2368MODULE_LICENSE("GPL v2");