Siddartha Mohanadoss | 899173a | 2013-01-29 19:18:50 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved. |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 13 | #include <linux/kernel.h> |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 14 | #include <linux/module.h> |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 15 | #include <linux/init.h> |
Siddartha Mohanadoss | eb90c92 | 2012-03-27 22:58:06 -0700 | [diff] [blame] | 16 | #include <linux/fs.h> |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 17 | #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 Mohanadoss | eb90c92 | 2012-03-27 22:58:06 -0700 | [diff] [blame] | 24 | #include <linux/uaccess.h> |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 25 | #include <linux/spi/spi.h> |
| 26 | #include <linux/hwmon-sysfs.h> |
Siddartha Mohanadoss | eb90c92 | 2012-03-27 22:58:06 -0700 | [diff] [blame] | 27 | #include <linux/miscdevice.h> |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 28 | #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 Mohanadoss | 3efc4d8 | 2013-06-11 15:34:52 -0700 | [diff] [blame] | 68 | #define GPIO_EPM_MARKER1 85 |
| 69 | #define GPIO_EPM_MARKER2 96 |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 70 | #define EPM_ADC_CONVERSION_TIME_MIN 50000 |
| 71 | #define EPM_ADC_CONVERSION_TIME_MAX 51000 |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 72 | /* 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 Mohanadoss | 46aff91 | 2013-07-10 18:53:48 -0700 | [diff] [blame] | 97 | #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 Mohanadoss | d34827d | 2013-06-12 09:57:30 -0700 | [diff] [blame] | 101 | #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 Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 105 | |
| 106 | #define EPM_PSOC_GLOBAL_ENABLE 81 |
| 107 | #define EPM_PSOC_VREF_VOLTAGE 2048 |
Siddartha Mohanadoss | ae7acf8 | 2012-12-12 10:36:07 -0800 | [diff] [blame] | 108 | #define EPM_PSOC_MAX_ADC_CODE_15_BIT 32767 |
| 109 | #define EPM_PSOC_MAX_ADC_CODE_12_BIT 4096 |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 110 | #define EPM_GLOBAL_ENABLE_MIN_DELAY 5000 |
| 111 | #define EPM_GLOBAL_ENABLE_MAX_DELAY 5100 |
| 112 | |
Siddartha Mohanadoss | 6e77b77 | 2012-09-19 21:58:13 -0700 | [diff] [blame] | 113 | #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 Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 124 | #define EPM_PSOC_BUFFERED_DATA_LENGTH 48 |
| 125 | #define EPM_PSOC_BUFFERED_DATA_LENGTH2 54 |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 126 | |
| 127 | struct epm_adc_drv { |
| 128 | struct platform_device *pdev; |
| 129 | struct device *hwmon; |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 130 | struct spi_device *epm_spi_client; |
| 131 | struct mutex conv_lock; |
| 132 | uint32_t bus_id; |
Siddartha Mohanadoss | eb90c92 | 2012-03-27 22:58:06 -0700 | [diff] [blame] | 133 | struct miscdevice misc; |
Siddartha Mohanadoss | 6e77b77 | 2012-09-19 21:58:13 -0700 | [diff] [blame] | 134 | uint32_t channel_mask; |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 135 | struct epm_chan_properties epm_psoc_ch_prop[0]; |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | static struct epm_adc_drv *epm_adc_drv; |
| 139 | static struct i2c_board_info *epm_i2c_info; |
| 140 | static bool epm_adc_first_request; |
| 141 | static int epm_gpio_expander_base_addr; |
Siddartha Mohanadoss | eb90c92 | 2012-03-27 22:58:06 -0700 | [diff] [blame] | 142 | static bool epm_adc_expander_register; |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 143 | |
| 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 | |
| 155 | static 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 | |
| 192 | static 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 Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 271 | pr_err("Set GPIO_EPM_SPI_ADC2_CS_N failed\n"); |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 272 | return rc; |
| 273 | } |
| 274 | } else { |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 275 | pr_err("gpio_request GPIO_EPM_SPI_ADC2_CS_N failed\n"); |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 276 | 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 | |
| 333 | static 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 | |
| 350 | static 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 | |
| 383 | static 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 | |
| 410 | static 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 | |
| 426 | static 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 | |
| 450 | static 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 | |
| 492 | static 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 | |
| 508 | epm_adc_hw_init_err: |
| 509 | mutex_unlock(&epm_adc->conv_lock); |
| 510 | return rc; |
| 511 | } |
| 512 | |
| 513 | static 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 Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 520 | pr_err("gpio expander disable failed with %d\n", rc); |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 521 | goto epm_adc_hw_deinit_err; |
| 522 | } |
| 523 | |
| 524 | epm_adc_hw_deinit_err: |
| 525 | mutex_unlock(&epm_adc->conv_lock); |
| 526 | return rc; |
| 527 | } |
| 528 | |
| 529 | static 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 Mohanadoss | de90ed4 | 2012-09-18 16:57:50 -0700 | [diff] [blame] | 537 | int64_t adc_scaled_data = 0; |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 538 | |
| 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 Mohanadoss | de90ed4 | 2012-09-18 16:57:50 -0700 | [diff] [blame] | 543 | adc_scaled_data = ((adc_raw_data[1] << 8) | adc_raw_data[2]); |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 544 | /* Obtain the internal system reading */ |
| 545 | if (channel_num == EPM_ADC_ADS_CHANNEL_VCC) { |
Siddartha Mohanadoss | de90ed4 | 2012-09-18 16:57:50 -0700 | [diff] [blame] | 546 | adc_scaled_data *= EPM_ADC_SCALE_MILLI; |
| 547 | do_div(adc_scaled_data, EPM_ADC_SCALE_CODE_VOLTS); |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 548 | } else if (channel_num == EPM_ADC_ADS_CHANNEL_GAIN) { |
Siddartha Mohanadoss | de90ed4 | 2012-09-18 16:57:50 -0700 | [diff] [blame] | 549 | do_div(adc_scaled_data, EPM_ADC_SCALE_CODE_GAIN); |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 550 | } else if (channel_num == EPM_ADC_ADS_CHANNEL_REF) { |
Siddartha Mohanadoss | de90ed4 | 2012-09-18 16:57:50 -0700 | [diff] [blame] | 551 | adc_scaled_data *= EPM_ADC_SCALE_MILLI; |
| 552 | do_div(adc_scaled_data, EPM_ADC_SCALE_CODE_VOLTS); |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 553 | } 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 Mohanadoss | de90ed4 | 2012-09-18 16:57:50 -0700 | [diff] [blame] | 556 | adc_scaled_data -= EPM_ADC_TEMP_TO_DEGC_COEFF; |
| 557 | do_div(adc_scaled_data, EPM_ADC_TEMP_SENSOR_COEFF); |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 558 | } 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 Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 566 | * mvVRef is in milli-volts and resistorvalue is in micro-ohms. |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 567 | * Hence, I = V/R gives us current in kilo-amps. |
| 568 | */ |
Siddartha Mohanadoss | de90ed4 | 2012-09-18 16:57:50 -0700 | [diff] [blame] | 569 | if (adc_scaled_data & EPM_ADC_MAX_NEGATIVE_SCALE_CODE) { |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 570 | sign_bit = -1; |
Siddartha Mohanadoss | de90ed4 | 2012-09-18 16:57:50 -0700 | [diff] [blame] | 571 | adc_scaled_data = (~adc_scaled_data |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 572 | & EPM_ADC_NEG_LSB_CODE); |
| 573 | } |
Siddartha Mohanadoss | de90ed4 | 2012-09-18 16:57:50 -0700 | [diff] [blame] | 574 | if (adc_scaled_data != 0) { |
| 575 | adc_scaled_data *= EPM_ADC_SCALE_FACTOR; |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 576 | /* Device is calibrated for 1LSB = VREF/7800h.*/ |
Siddartha Mohanadoss | de90ed4 | 2012-09-18 16:57:50 -0700 | [diff] [blame] | 577 | adc_scaled_data *= EPM_ADC_MILLI_VOLTS_SOURCE; |
| 578 | do_div(adc_scaled_data, EPM_ADC_VREF_CODE); |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 579 | /* Data will now be in micro-volts.*/ |
Siddartha Mohanadoss | de90ed4 | 2012-09-18 16:57:50 -0700 | [diff] [blame] | 580 | adc_scaled_data *= EPM_ADC_SCALE_MILLI; |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 581 | /* Divide by amplifier gain value.*/ |
Siddartha Mohanadoss | de90ed4 | 2012-09-18 16:57:50 -0700 | [diff] [blame] | 582 | do_div(adc_scaled_data, pdata->channel[chan_idx].gain); |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 583 | /* Data will now be in nano-volts.*/ |
Siddartha Mohanadoss | de90ed4 | 2012-09-18 16:57:50 -0700 | [diff] [blame] | 584 | do_div(adc_scaled_data, EPM_ADC_SCALE_FACTOR); |
| 585 | adc_scaled_data *= EPM_ADC_SCALE_MILLI; |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 586 | /* Data is now in micro-amps.*/ |
Siddartha Mohanadoss | de90ed4 | 2012-09-18 16:57:50 -0700 | [diff] [blame] | 587 | do_div(adc_scaled_data, |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 588 | pdata->channel[chan_idx].resistorvalue); |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 589 | /* Set the sign bit for lekage current. */ |
Siddartha Mohanadoss | de90ed4 | 2012-09-18 16:57:50 -0700 | [diff] [blame] | 590 | adc_scaled_data *= sign_bit; |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 591 | } |
| 592 | } |
Siddartha Mohanadoss | 6e77b77 | 2012-09-19 21:58:13 -0700 | [diff] [blame] | 593 | |
Siddartha Mohanadoss | de90ed4 | 2012-09-18 16:57:50 -0700 | [diff] [blame] | 594 | conv->physical = (int32_t) adc_scaled_data; |
Yan He | f6adab7 | 2012-08-31 11:09:36 -0700 | [diff] [blame] | 595 | |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 596 | return 0; |
| 597 | } |
| 598 | |
Siddartha Mohanadoss | 899173a | 2013-01-29 19:18:50 -0800 | [diff] [blame] | 599 | static int epm_psoc_scale_result(int16_t result, uint32_t index) |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 600 | { |
| 601 | struct epm_adc_drv *epm_adc = epm_adc_drv; |
Siddartha Mohanadoss | 899173a | 2013-01-29 19:18:50 -0800 | [diff] [blame] | 602 | int32_t result_cur, neg = 0; |
Siddartha Mohanadoss | 6e77b77 | 2012-09-19 21:58:13 -0700 | [diff] [blame] | 603 | |
Siddartha Mohanadoss | ae7acf8 | 2012-12-12 10:36:07 -0800 | [diff] [blame] | 604 | if ((1 << index) & epm_adc->channel_mask) { |
Siddartha Mohanadoss | 899173a | 2013-01-29 19:18:50 -0800 | [diff] [blame] | 605 | if (result & 0x800) { |
| 606 | neg = 1; |
| 607 | result = result & 0x7ff; |
| 608 | } |
Siddartha Mohanadoss | ae7acf8 | 2012-12-12 10:36:07 -0800 | [diff] [blame] | 609 | /* result = (2.048V * code)/(4096 * gain * rsense) */ |
| 610 | result_cur = ((EPM_PSOC_VREF_VOLTAGE * result)/ |
| 611 | EPM_PSOC_MAX_ADC_CODE_12_BIT); |
Siddartha Mohanadoss | 6e77b77 | 2012-09-19 21:58:13 -0700 | [diff] [blame] | 612 | |
Siddartha Mohanadoss | ae7acf8 | 2012-12-12 10:36:07 -0800 | [diff] [blame] | 613 | result_cur = (result_cur/ |
| 614 | (epm_adc->epm_psoc_ch_prop[index].gain * |
| 615 | epm_adc->epm_psoc_ch_prop[index].resistorvalue)); |
Siddartha Mohanadoss | 899173a | 2013-01-29 19:18:50 -0800 | [diff] [blame] | 616 | if (neg) |
| 617 | result_cur -= result_cur; |
Siddartha Mohanadoss | ae7acf8 | 2012-12-12 10:36:07 -0800 | [diff] [blame] | 618 | } else { |
Siddartha Mohanadoss | 899173a | 2013-01-29 19:18:50 -0800 | [diff] [blame] | 619 | if (result & 0x8000) { |
| 620 | neg = 1; |
| 621 | result = result & 0x7fff; |
| 622 | } |
Siddartha Mohanadoss | ae7acf8 | 2012-12-12 10:36:07 -0800 | [diff] [blame] | 623 | /* result = (2.048V * code)/(32767 * gain * rsense) */ |
Siddartha Mohanadoss | 899173a | 2013-01-29 19:18:50 -0800 | [diff] [blame] | 624 | result_cur = (((EPM_PSOC_VREF_VOLTAGE * (int) result)/ |
Siddartha Mohanadoss | ae7acf8 | 2012-12-12 10:36:07 -0800 | [diff] [blame] | 625 | EPM_PSOC_MAX_ADC_CODE_15_BIT) * 1000); |
| 626 | |
| 627 | result_cur = (result_cur/ |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 628 | (epm_adc->epm_psoc_ch_prop[index].gain * |
Siddartha Mohanadoss | 6e77b77 | 2012-09-19 21:58:13 -0700 | [diff] [blame] | 629 | epm_adc->epm_psoc_ch_prop[index].resistorvalue)); |
Siddartha Mohanadoss | 899173a | 2013-01-29 19:18:50 -0800 | [diff] [blame] | 630 | if (neg) |
| 631 | result_cur -= result_cur; |
Siddartha Mohanadoss | ae7acf8 | 2012-12-12 10:36:07 -0800 | [diff] [blame] | 632 | } |
Siddartha Mohanadoss | 6e77b77 | 2012-09-19 21:58:13 -0700 | [diff] [blame] | 633 | |
| 634 | return result_cur; |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 635 | } |
| 636 | |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 637 | static 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); |
| 702 | conv_err: |
| 703 | mutex_unlock(&epm_adc->conv_lock); |
| 704 | return rc; |
| 705 | } |
| 706 | |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 707 | static 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 Mohanadoss | 3efc4d8 | 2013-06-11 15:34:52 -0700 | [diff] [blame] | 728 | static 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 | |
| 746 | static 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 | |
| 764 | static int epm_marker1_release(void) |
| 765 | { |
| 766 | gpio_free(GPIO_EPM_MARKER1); |
| 767 | |
| 768 | return 0; |
| 769 | } |
| 770 | |
| 771 | static int epm_marker2_release(void) |
| 772 | { |
| 773 | gpio_free(GPIO_EPM_MARKER2); |
| 774 | |
| 775 | return 0; |
| 776 | } |
| 777 | |
Siddartha Mohanadoss | 46aff91 | 2013-07-10 18:53:48 -0700 | [diff] [blame] | 778 | static 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 | |
| 815 | static 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 Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 852 | static 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 Mohanadoss | 6e77b77 | 2012-09-19 21:58:13 -0700 | [diff] [blame] | 879 | rc = spi_sync(epm_adc->epm_spi_client, &m); |
| 880 | if (rc) |
| 881 | return rc; |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 882 | 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 | |
| 891 | return rc; |
| 892 | } |
| 893 | |
| 894 | static int epm_psoc_channel_configure(struct epm_adc_drv *epm_adc, |
| 895 | struct epm_psoc_channel_configure *psoc_chan_configure) |
| 896 | { |
| 897 | struct spi_message m; |
| 898 | struct spi_transfer t; |
| 899 | char tx_buf[9], rx_buf[9]; |
| 900 | int32_t rc = 0, chan_num; |
| 901 | |
| 902 | spi_setup(epm_adc->epm_spi_client); |
| 903 | |
| 904 | memset(&t, 0, sizeof t); |
| 905 | memset(tx_buf, 0, sizeof tx_buf); |
| 906 | memset(rx_buf, 0, sizeof tx_buf); |
| 907 | t.tx_buf = tx_buf; |
| 908 | t.rx_buf = rx_buf; |
| 909 | spi_message_init(&m); |
| 910 | spi_message_add_tail(&t, &m); |
| 911 | |
| 912 | chan_num = psoc_chan_configure->channel_num; |
| 913 | |
| 914 | tx_buf[0] = psoc_chan_configure->cmd; |
| 915 | tx_buf[1] = 0; |
| 916 | tx_buf[2] = (chan_num & 0xff000000) >> 24; |
| 917 | tx_buf[3] = (chan_num & 0xff0000) >> 16; |
| 918 | tx_buf[4] = (chan_num & 0xff00) >> 8; |
| 919 | tx_buf[5] = (chan_num & 0xff); |
| 920 | |
| 921 | t.len = sizeof(tx_buf); |
| 922 | t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD; |
| 923 | |
| 924 | rc = spi_sync(epm_adc->epm_spi_client, &m); |
| 925 | if (rc) |
| 926 | return rc; |
| 927 | |
| 928 | rc = spi_sync(epm_adc->epm_spi_client, &m); |
| 929 | if (rc) |
| 930 | return rc; |
| 931 | |
| 932 | psoc_chan_configure->cmd = rx_buf[0]; |
| 933 | psoc_chan_configure->device_num = rx_buf[1]; |
| 934 | chan_num = rx_buf[2] << 24 | (rx_buf[3] << 16) | (rx_buf[4] << 8) | |
| 935 | rx_buf[5]; |
| 936 | psoc_chan_configure->channel_num = chan_num; |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 937 | |
| 938 | return rc; |
| 939 | } |
| 940 | |
| 941 | static int epm_psoc_set_averaging(struct epm_adc_drv *epm_adc, |
| 942 | struct epm_psoc_set_avg *psoc_set_avg) |
| 943 | { |
| 944 | struct spi_message m; |
| 945 | struct spi_transfer t; |
| 946 | char tx_buf[4], rx_buf[4]; |
| 947 | int rc = 0; |
| 948 | |
| 949 | spi_setup(epm_adc->epm_spi_client); |
| 950 | |
| 951 | memset(&t, 0, sizeof t); |
| 952 | memset(tx_buf, 0, sizeof tx_buf); |
| 953 | memset(rx_buf, 0, sizeof tx_buf); |
| 954 | t.tx_buf = tx_buf; |
| 955 | t.rx_buf = rx_buf; |
| 956 | spi_message_init(&m); |
| 957 | spi_message_add_tail(&t, &m); |
| 958 | |
| 959 | tx_buf[0] = psoc_set_avg->cmd; |
| 960 | tx_buf[1] = psoc_set_avg->avg_period; |
| 961 | |
| 962 | t.len = sizeof(tx_buf); |
| 963 | t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD; |
| 964 | |
| 965 | rc = spi_sync(epm_adc->epm_spi_client, &m); |
| 966 | if (rc) |
| 967 | return rc; |
| 968 | |
| 969 | rc = spi_sync(epm_adc->epm_spi_client, &m); |
| 970 | if (rc) |
| 971 | return rc; |
| 972 | |
| 973 | psoc_set_avg->cmd = rx_buf[0]; |
| 974 | psoc_set_avg->return_code = rx_buf[1]; |
| 975 | |
| 976 | return rc; |
| 977 | } |
| 978 | |
| 979 | static int epm_psoc_get_data(struct epm_adc_drv *epm_adc, |
| 980 | struct epm_psoc_get_data *psoc_get_meas) |
| 981 | { |
| 982 | struct spi_message m; |
| 983 | struct spi_transfer t; |
| 984 | char tx_buf[10], rx_buf[10]; |
| 985 | int rc = 0; |
| 986 | |
| 987 | spi_setup(epm_adc->epm_spi_client); |
| 988 | |
| 989 | memset(&t, 0, sizeof t); |
| 990 | memset(tx_buf, 0, sizeof tx_buf); |
| 991 | memset(rx_buf, 0, sizeof tx_buf); |
| 992 | t.tx_buf = tx_buf; |
| 993 | t.rx_buf = rx_buf; |
| 994 | spi_message_init(&m); |
| 995 | spi_message_add_tail(&t, &m); |
| 996 | |
| 997 | tx_buf[0] = psoc_get_meas->cmd; |
| 998 | tx_buf[1] = psoc_get_meas->dev_num; |
| 999 | tx_buf[2] = psoc_get_meas->chan_num; |
| 1000 | |
| 1001 | t.len = sizeof(tx_buf); |
| 1002 | t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD; |
| 1003 | |
| 1004 | rc = spi_sync(epm_adc->epm_spi_client, &m); |
| 1005 | if (rc) |
| 1006 | return rc; |
| 1007 | |
| 1008 | rc = spi_sync(epm_adc->epm_spi_client, &m); |
| 1009 | if (rc) |
| 1010 | return rc; |
| 1011 | |
| 1012 | psoc_get_meas->cmd = rx_buf[0]; |
| 1013 | psoc_get_meas->dev_num = rx_buf[1]; |
| 1014 | psoc_get_meas->chan_num = rx_buf[2]; |
| 1015 | psoc_get_meas->timestamp_resp_value = (rx_buf[3] << 24) | |
| 1016 | (rx_buf[4] << 16) | (rx_buf[5] << 8) | |
| 1017 | rx_buf[6]; |
Siddartha Mohanadoss | 899173a | 2013-01-29 19:18:50 -0800 | [diff] [blame] | 1018 | psoc_get_meas->reading_raw = (rx_buf[7] << 8) | rx_buf[8]; |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 1019 | |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 1020 | return rc; |
| 1021 | } |
| 1022 | |
| 1023 | static int epm_psoc_get_buffered_data(struct epm_adc_drv *epm_adc, |
| 1024 | struct epm_psoc_get_buffered_data *psoc_get_meas) |
| 1025 | { |
| 1026 | struct spi_message m; |
| 1027 | struct spi_transfer t; |
| 1028 | char tx_buf[64], rx_buf[64]; |
| 1029 | int rc = 0, i; |
| 1030 | |
| 1031 | spi_setup(epm_adc->epm_spi_client); |
| 1032 | |
| 1033 | memset(&t, 0, sizeof t); |
| 1034 | memset(tx_buf, 0, sizeof tx_buf); |
| 1035 | memset(rx_buf, 0, sizeof tx_buf); |
| 1036 | t.tx_buf = tx_buf; |
| 1037 | t.rx_buf = rx_buf; |
| 1038 | spi_message_init(&m); |
| 1039 | spi_message_add_tail(&t, &m); |
| 1040 | |
| 1041 | tx_buf[0] = psoc_get_meas->cmd; |
| 1042 | |
| 1043 | t.len = sizeof(tx_buf); |
| 1044 | t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD; |
| 1045 | |
| 1046 | rc = spi_sync(epm_adc->epm_spi_client, &m); |
| 1047 | if (rc) |
| 1048 | return rc; |
| 1049 | |
| 1050 | rc = spi_sync(epm_adc->epm_spi_client, &m); |
| 1051 | if (rc) |
| 1052 | return rc; |
| 1053 | |
| 1054 | psoc_get_meas->cmd = rx_buf[0]; |
| 1055 | psoc_get_meas->dev_num = rx_buf[1]; |
| 1056 | psoc_get_meas->status_mask = rx_buf[2]; |
| 1057 | psoc_get_meas->chan_idx = rx_buf[3]; |
| 1058 | psoc_get_meas->chan_mask = (rx_buf[4] << 24 | |
| 1059 | rx_buf[5] << 16 | rx_buf[6] << 8 |
| 1060 | | rx_buf[7]); |
| 1061 | psoc_get_meas->timestamp_start = (rx_buf[8] << 24 | |
| 1062 | rx_buf[9] << 16 | rx_buf[10] << 8 |
| 1063 | | rx_buf[11]); |
| 1064 | psoc_get_meas->timestamp_end = (rx_buf[12] << 24 | |
| 1065 | rx_buf[13] << 16 | rx_buf[14] << 8 |
| 1066 | | rx_buf[15]); |
| 1067 | |
| 1068 | for (i = 0; i < EPM_PSOC_BUFFERED_DATA_LENGTH; i++) |
| 1069 | psoc_get_meas->buff_data[i] = rx_buf[16 + i]; |
| 1070 | |
| 1071 | return rc; |
| 1072 | } |
| 1073 | |
Siddartha Mohanadoss | 278815e | 2013-04-22 11:18:17 -0700 | [diff] [blame] | 1074 | static int epm_psoc_get_timestamp(struct epm_adc_drv *epm_adc, |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 1075 | struct epm_psoc_system_time_stamp *psoc_timestamp) |
| 1076 | { |
| 1077 | struct spi_message m; |
| 1078 | struct spi_transfer t; |
| 1079 | char tx_buf[10], rx_buf[10]; |
| 1080 | int rc = 0; |
| 1081 | |
| 1082 | spi_setup(epm_adc->epm_spi_client); |
| 1083 | |
| 1084 | memset(&t, 0, sizeof t); |
| 1085 | memset(tx_buf, 0, sizeof tx_buf); |
| 1086 | memset(rx_buf, 0, sizeof tx_buf); |
| 1087 | t.tx_buf = tx_buf; |
| 1088 | t.rx_buf = rx_buf; |
| 1089 | spi_message_init(&m); |
| 1090 | spi_message_add_tail(&t, &m); |
| 1091 | |
Siddartha Mohanadoss | 278815e | 2013-04-22 11:18:17 -0700 | [diff] [blame] | 1092 | psoc_timestamp->cmd = EPM_PSOC_GET_SYSTEM_TIMESTAMP_CMD; |
| 1093 | tx_buf[0] = psoc_timestamp->cmd; |
| 1094 | |
| 1095 | t.len = sizeof(tx_buf); |
| 1096 | t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD; |
| 1097 | |
| 1098 | rc = spi_sync(epm_adc->epm_spi_client, &m); |
| 1099 | if (rc) |
| 1100 | return rc; |
| 1101 | |
| 1102 | rc = spi_sync(epm_adc->epm_spi_client, &m); |
| 1103 | if (rc) |
| 1104 | return rc; |
| 1105 | |
| 1106 | psoc_timestamp->cmd = rx_buf[0]; |
| 1107 | psoc_timestamp->timestamp = rx_buf[1] << 24 | rx_buf[2] << 16 | |
| 1108 | rx_buf[3] << 8 | rx_buf[4]; |
| 1109 | |
| 1110 | return rc; |
| 1111 | } |
| 1112 | |
| 1113 | static int epm_psoc_set_timestamp(struct epm_adc_drv *epm_adc, |
| 1114 | struct epm_psoc_system_time_stamp *psoc_timestamp) |
| 1115 | { |
| 1116 | struct spi_message m; |
| 1117 | struct spi_transfer t; |
| 1118 | char tx_buf[10], rx_buf[10]; |
| 1119 | int rc = 0; |
| 1120 | |
| 1121 | spi_setup(epm_adc->epm_spi_client); |
| 1122 | |
| 1123 | memset(&t, 0, sizeof t); |
| 1124 | memset(tx_buf, 0, sizeof tx_buf); |
| 1125 | memset(rx_buf, 0, sizeof tx_buf); |
| 1126 | t.tx_buf = tx_buf; |
| 1127 | t.rx_buf = rx_buf; |
| 1128 | spi_message_init(&m); |
| 1129 | spi_message_add_tail(&t, &m); |
| 1130 | |
| 1131 | psoc_timestamp->cmd = EPM_PSOC_SET_SYSTEM_TIMESTAMP_CMD; |
| 1132 | tx_buf[0] = psoc_timestamp->cmd; |
| 1133 | tx_buf[1] = (psoc_timestamp->timestamp >> 24) & 0xff; |
| 1134 | tx_buf[2] = (psoc_timestamp->timestamp >> 16) & 0xff; |
| 1135 | tx_buf[3] = (psoc_timestamp->timestamp >> 8) & 0xff; |
| 1136 | tx_buf[4] = (psoc_timestamp->timestamp & 0xff); |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 1137 | |
| 1138 | t.len = sizeof(tx_buf); |
| 1139 | t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD; |
| 1140 | |
| 1141 | rc = spi_sync(epm_adc->epm_spi_client, &m); |
| 1142 | if (rc) |
| 1143 | return rc; |
| 1144 | |
| 1145 | rc = spi_sync(epm_adc->epm_spi_client, &m); |
| 1146 | if (rc) |
| 1147 | return rc; |
| 1148 | |
| 1149 | psoc_timestamp->cmd = rx_buf[0]; |
| 1150 | psoc_timestamp->timestamp = rx_buf[1] << 24 | rx_buf[2] << 16 | |
| 1151 | rx_buf[3] << 8 | rx_buf[4]; |
| 1152 | |
| 1153 | return rc; |
| 1154 | } |
| 1155 | |
| 1156 | static int epm_psoc_get_avg_buffered_switch_data(struct epm_adc_drv *epm_adc, |
| 1157 | struct epm_psoc_get_avg_buffered_switch_data *psoc_get_meas) |
| 1158 | { |
| 1159 | struct spi_message m; |
| 1160 | struct spi_transfer t; |
| 1161 | char tx_buf[64], rx_buf[64]; |
Siddartha Mohanadoss | 6e77b77 | 2012-09-19 21:58:13 -0700 | [diff] [blame] | 1162 | int rc = 0, i = 0, j = 0, z = 0; |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 1163 | |
| 1164 | spi_setup(epm_adc->epm_spi_client); |
| 1165 | |
| 1166 | memset(&t, 0, sizeof t); |
| 1167 | memset(tx_buf, 0, sizeof tx_buf); |
| 1168 | memset(rx_buf, 0, sizeof tx_buf); |
| 1169 | t.tx_buf = tx_buf; |
| 1170 | t.rx_buf = rx_buf; |
| 1171 | spi_message_init(&m); |
| 1172 | spi_message_add_tail(&t, &m); |
| 1173 | |
| 1174 | tx_buf[0] = psoc_get_meas->cmd; |
| 1175 | |
| 1176 | t.len = sizeof(tx_buf); |
| 1177 | t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD; |
| 1178 | |
| 1179 | rc = spi_sync(epm_adc->epm_spi_client, &m); |
| 1180 | if (rc) |
| 1181 | return rc; |
| 1182 | |
| 1183 | rc = spi_sync(epm_adc->epm_spi_client, &m); |
| 1184 | if (rc) |
| 1185 | return rc; |
| 1186 | |
| 1187 | psoc_get_meas->cmd = rx_buf[0]; |
| 1188 | psoc_get_meas->status = rx_buf[1]; |
| 1189 | psoc_get_meas->timestamp_start = (rx_buf[2] << 24 | |
| 1190 | rx_buf[3] << 16 | rx_buf[4] << 8 |
| 1191 | | rx_buf[5]); |
| 1192 | psoc_get_meas->channel_mask = (rx_buf[6] << 24 | |
| 1193 | rx_buf[7] << 16 | rx_buf[8] << 8 |
| 1194 | | rx_buf[9]); |
| 1195 | |
| 1196 | for (i = 0; i < EPM_PSOC_BUFFERED_DATA_LENGTH2; i++) |
| 1197 | psoc_get_meas->avg_data[i] = rx_buf[10 + i]; |
| 1198 | |
Siddartha Mohanadoss | 6e77b77 | 2012-09-19 21:58:13 -0700 | [diff] [blame] | 1199 | i = j = 0; |
| 1200 | for (z = 0; z < 4; z++) { |
| 1201 | psoc_get_meas->data[i].channel = i; |
| 1202 | psoc_get_meas->data[i].avg_buffer_sample = |
| 1203 | rx_buf[10 + j] & EPM_AVG_BUF_MASK1; |
| 1204 | i++; |
| 1205 | j++; |
| 1206 | psoc_get_meas->data[i].avg_buffer_sample = |
| 1207 | rx_buf[10 + j] & EPM_AVG_BUF_MASK2; |
| 1208 | i++; |
| 1209 | j++; |
| 1210 | psoc_get_meas->data[i].avg_buffer_sample = |
| 1211 | rx_buf[10 + j] & EPM_AVG_BUF_MASK3; |
| 1212 | psoc_get_meas->data[i].avg_buffer_sample <<= 8; |
| 1213 | j++; |
| 1214 | psoc_get_meas->data[i].avg_buffer_sample = |
| 1215 | psoc_get_meas->data[i].avg_buffer_sample | |
| 1216 | (rx_buf[10 + j] & EPM_AVG_BUF_MASK4); |
| 1217 | i++; |
| 1218 | j++; |
| 1219 | psoc_get_meas->data[i].avg_buffer_sample = |
| 1220 | rx_buf[10 + j] & EPM_AVG_BUF_MASK5; |
| 1221 | i++; |
| 1222 | j++; |
| 1223 | psoc_get_meas->data[i].avg_buffer_sample = |
| 1224 | rx_buf[10 + j] & EPM_AVG_BUF_MASK6; |
| 1225 | i++; |
| 1226 | j++; |
| 1227 | psoc_get_meas->data[i].avg_buffer_sample = |
| 1228 | rx_buf[10 + j] & EPM_AVG_BUF_MASK7; |
| 1229 | psoc_get_meas->data[i].avg_buffer_sample <<= 4; |
| 1230 | j++; |
| 1231 | psoc_get_meas->data[i].avg_buffer_sample = |
| 1232 | psoc_get_meas->data[i].avg_buffer_sample | |
| 1233 | (rx_buf[10 + j] & EPM_AVG_BUF_MASK8); |
| 1234 | i++; |
| 1235 | j++; |
| 1236 | psoc_get_meas->data[i].avg_buffer_sample = |
| 1237 | rx_buf[10 + j] & EPM_AVG_BUF_MASK9; |
| 1238 | i++; |
| 1239 | j++; |
| 1240 | psoc_get_meas->data[i].avg_buffer_sample = |
| 1241 | rx_buf[10 + j] & EPM_AVG_BUF_MASK10; |
| 1242 | } |
| 1243 | |
| 1244 | for (z = 0; z < 32; z++) { |
| 1245 | if (psoc_get_meas->data[z].avg_buffer_sample != 0) |
| 1246 | psoc_get_meas->data[z].result = epm_psoc_scale_result( |
| 1247 | psoc_get_meas->data[z].avg_buffer_sample, z); |
| 1248 | } |
| 1249 | |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 1250 | return rc; |
| 1251 | } |
| 1252 | |
| 1253 | static int epm_psoc_set_vadc(struct epm_adc_drv *epm_adc, |
| 1254 | struct epm_psoc_set_vadc *psoc_set_vadc) |
| 1255 | { |
| 1256 | struct spi_message m; |
| 1257 | struct spi_transfer t; |
| 1258 | char tx_buf[10], rx_buf[10]; |
| 1259 | int rc = 0; |
| 1260 | |
| 1261 | spi_setup(epm_adc->epm_spi_client); |
| 1262 | |
| 1263 | memset(&t, 0, sizeof t); |
| 1264 | memset(tx_buf, 0, sizeof tx_buf); |
| 1265 | memset(rx_buf, 0, sizeof tx_buf); |
| 1266 | t.tx_buf = tx_buf; |
| 1267 | t.rx_buf = rx_buf; |
| 1268 | spi_message_init(&m); |
| 1269 | spi_message_add_tail(&t, &m); |
| 1270 | |
| 1271 | tx_buf[0] = psoc_set_vadc->cmd; |
| 1272 | tx_buf[1] = psoc_set_vadc->vadc_dev; |
| 1273 | tx_buf[2] = (psoc_set_vadc->vadc_voltage & 0xff000000) >> 24; |
| 1274 | tx_buf[3] = (psoc_set_vadc->vadc_voltage & 0xff0000) >> 16; |
| 1275 | tx_buf[4] = (psoc_set_vadc->vadc_voltage & 0xff00) >> 8; |
| 1276 | tx_buf[5] = psoc_set_vadc->vadc_voltage & 0xff; |
| 1277 | |
| 1278 | t.len = sizeof(tx_buf); |
| 1279 | t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD; |
| 1280 | |
| 1281 | rc = spi_sync(epm_adc->epm_spi_client, &m); |
| 1282 | if (rc) |
| 1283 | return rc; |
| 1284 | |
| 1285 | rc = spi_sync(epm_adc->epm_spi_client, &m); |
| 1286 | if (rc) |
| 1287 | return rc; |
| 1288 | |
| 1289 | psoc_set_vadc->cmd = rx_buf[0]; |
| 1290 | psoc_set_vadc->vadc_dev = rx_buf[1]; |
| 1291 | psoc_set_vadc->vadc_voltage = (rx_buf[2] << 24) | (rx_buf[3] << 16) | |
| 1292 | (rx_buf[4] << 8) | (rx_buf[5]); |
| 1293 | |
| 1294 | return rc; |
| 1295 | } |
| 1296 | |
| 1297 | static int epm_psoc_set_channel_switch(struct epm_adc_drv *epm_adc, |
| 1298 | struct epm_psoc_set_channel_switch *psoc_channel_switch) |
| 1299 | { |
| 1300 | struct spi_message m; |
| 1301 | struct spi_transfer t; |
| 1302 | char tx_buf[10], rx_buf[10]; |
| 1303 | int rc = 0; |
| 1304 | |
| 1305 | spi_setup(epm_adc->epm_spi_client); |
| 1306 | |
| 1307 | memset(&t, 0, sizeof t); |
| 1308 | memset(tx_buf, 0, sizeof tx_buf); |
| 1309 | memset(rx_buf, 0, sizeof tx_buf); |
| 1310 | t.tx_buf = tx_buf; |
| 1311 | t.rx_buf = rx_buf; |
| 1312 | spi_message_init(&m); |
| 1313 | spi_message_add_tail(&t, &m); |
| 1314 | |
| 1315 | tx_buf[0] = psoc_channel_switch->cmd; |
| 1316 | tx_buf[1] = psoc_channel_switch->dev; |
| 1317 | tx_buf[2] = (psoc_channel_switch->delay & 0xff000000) >> 24; |
| 1318 | tx_buf[3] = (psoc_channel_switch->delay & 0xff0000) >> 16; |
| 1319 | tx_buf[4] = (psoc_channel_switch->delay & 0xff00) >> 8; |
| 1320 | tx_buf[5] = psoc_channel_switch->delay & 0xff; |
| 1321 | |
| 1322 | t.len = sizeof(tx_buf); |
| 1323 | t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD; |
| 1324 | |
| 1325 | rc = spi_sync(epm_adc->epm_spi_client, &m); |
| 1326 | if (rc) |
| 1327 | return rc; |
| 1328 | |
| 1329 | rc = spi_sync(epm_adc->epm_spi_client, &m); |
| 1330 | if (rc) |
| 1331 | return rc; |
| 1332 | |
| 1333 | psoc_channel_switch->cmd = rx_buf[0]; |
| 1334 | psoc_channel_switch->dev = rx_buf[1]; |
| 1335 | psoc_channel_switch->delay = rx_buf[2] << 24 | |
| 1336 | rx_buf[3] << 16 | |
| 1337 | rx_buf[4] << 8 | rx_buf[5]; |
| 1338 | |
| 1339 | return rc; |
| 1340 | } |
| 1341 | |
| 1342 | static int epm_psoc_clear_buffer(struct epm_adc_drv *epm_adc) |
| 1343 | { |
| 1344 | struct spi_message m; |
| 1345 | struct spi_transfer t; |
| 1346 | char tx_buf[3], rx_buf[3]; |
| 1347 | int rc = 0; |
| 1348 | |
| 1349 | spi_setup(epm_adc->epm_spi_client); |
| 1350 | |
| 1351 | memset(&t, 0, sizeof t); |
| 1352 | memset(tx_buf, 0, sizeof tx_buf); |
| 1353 | memset(rx_buf, 0, sizeof tx_buf); |
| 1354 | t.tx_buf = tx_buf; |
| 1355 | t.rx_buf = rx_buf; |
| 1356 | spi_message_init(&m); |
| 1357 | spi_message_add_tail(&t, &m); |
| 1358 | |
| 1359 | tx_buf[0] = EPM_PSOC_CLEAR_BUFFER_CMD; |
| 1360 | |
| 1361 | t.len = sizeof(tx_buf); |
| 1362 | t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD; |
| 1363 | |
| 1364 | rc = spi_sync(epm_adc->epm_spi_client, &m); |
| 1365 | if (rc) |
| 1366 | return rc; |
| 1367 | |
| 1368 | rc = spi_sync(epm_adc->epm_spi_client, &m); |
| 1369 | if (rc) |
| 1370 | return rc; |
| 1371 | |
Siddartha Mohanadoss | 3ff2063 | 2013-05-17 10:53:03 -0700 | [diff] [blame] | 1372 | rc = rx_buf[1]; |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 1373 | |
| 1374 | return rc; |
| 1375 | } |
| 1376 | |
Siddartha Mohanadoss | d34827d | 2013-06-12 09:57:30 -0700 | [diff] [blame] | 1377 | static int epm_psoc_get_gpio_buffer_data(struct epm_adc_drv *epm_adc, |
| 1378 | struct epm_get_gpio_buffer_resp *gpio_resp_pkt) |
| 1379 | { |
| 1380 | struct spi_message m; |
| 1381 | struct spi_transfer t; |
| 1382 | char tx_buf[7], rx_buf[7]; |
| 1383 | int rc = 0; |
| 1384 | |
| 1385 | spi_setup(epm_adc->epm_spi_client); |
| 1386 | |
| 1387 | memset(&t, 0, sizeof t); |
| 1388 | memset(tx_buf, 0, sizeof tx_buf); |
| 1389 | memset(rx_buf, 0, sizeof tx_buf); |
| 1390 | t.tx_buf = tx_buf; |
| 1391 | t.rx_buf = rx_buf; |
| 1392 | spi_message_init(&m); |
| 1393 | spi_message_add_tail(&t, &m); |
| 1394 | |
| 1395 | tx_buf[0] = EPM_PSOC_GET_GPIO_BUFFER_CMD; |
| 1396 | |
| 1397 | t.len = sizeof(tx_buf); |
| 1398 | t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD; |
| 1399 | |
| 1400 | rc = spi_sync(epm_adc->epm_spi_client, &m); |
| 1401 | if (rc) |
| 1402 | return rc; |
| 1403 | |
| 1404 | rc = spi_sync(epm_adc->epm_spi_client, &m); |
| 1405 | if (rc) |
| 1406 | return rc; |
| 1407 | |
| 1408 | gpio_resp_pkt->cmd = rx_buf[0]; |
| 1409 | gpio_resp_pkt->status = rx_buf[1]; |
| 1410 | gpio_resp_pkt->bitmask_monitor_pin = rx_buf[2]; |
| 1411 | gpio_resp_pkt->timestamp = rx_buf[3] << 24 | rx_buf[4] << 16 | |
| 1412 | rx_buf[5] << 8 | tx_buf[6]; |
| 1413 | |
| 1414 | return rc; |
| 1415 | } |
| 1416 | |
| 1417 | static int epm_psoc_gpio_buffer_request_configure(struct epm_adc_drv *epm_adc, |
| 1418 | struct epm_gpio_buffer_request *gpio_request) |
| 1419 | { |
| 1420 | struct spi_message m; |
| 1421 | struct spi_transfer t; |
| 1422 | char tx_buf[2], rx_buf[2]; |
| 1423 | int rc = 0; |
| 1424 | |
| 1425 | spi_setup(epm_adc->epm_spi_client); |
| 1426 | |
| 1427 | memset(&t, 0, sizeof t); |
| 1428 | memset(tx_buf, 0, sizeof tx_buf); |
| 1429 | memset(rx_buf, 0, sizeof tx_buf); |
| 1430 | t.tx_buf = tx_buf; |
| 1431 | t.rx_buf = rx_buf; |
| 1432 | spi_message_init(&m); |
| 1433 | spi_message_add_tail(&t, &m); |
| 1434 | |
| 1435 | tx_buf[0] = EPM_PSOC_GPIO_BUFFER_REQUEST_CMD; |
| 1436 | tx_buf[1] = gpio_request->bitmask_monitor_pin; |
| 1437 | |
| 1438 | t.len = sizeof(tx_buf); |
| 1439 | t.bits_per_word = EPM_ADC_ADS_SPI_BITS_PER_WORD; |
| 1440 | |
| 1441 | rc = spi_sync(epm_adc->epm_spi_client, &m); |
| 1442 | if (rc) |
| 1443 | return rc; |
| 1444 | |
| 1445 | rc = spi_sync(epm_adc->epm_spi_client, &m); |
| 1446 | if (rc) |
| 1447 | return rc; |
| 1448 | |
| 1449 | gpio_request->cmd = rx_buf[0]; |
| 1450 | gpio_request->status = rx_buf[1]; |
| 1451 | |
| 1452 | return rc; |
| 1453 | } |
| 1454 | |
Siddartha Mohanadoss | eb90c92 | 2012-03-27 22:58:06 -0700 | [diff] [blame] | 1455 | static long epm_adc_ioctl(struct file *file, unsigned int cmd, |
| 1456 | unsigned long arg) |
| 1457 | { |
| 1458 | struct epm_adc_drv *epm_adc = epm_adc_drv; |
| 1459 | |
| 1460 | switch (cmd) { |
| 1461 | case EPM_ADC_REQUEST: |
| 1462 | { |
| 1463 | struct epm_chan_request conv; |
| 1464 | int rc; |
| 1465 | |
| 1466 | if (copy_from_user(&conv, (void __user *)arg, |
| 1467 | sizeof(struct epm_chan_request))) |
| 1468 | return -EFAULT; |
| 1469 | |
| 1470 | rc = epm_adc_blocking_conversion(epm_adc, &conv); |
| 1471 | if (rc) { |
| 1472 | pr_err("Failed EPM conversion:%d\n", rc); |
| 1473 | return rc; |
| 1474 | } |
| 1475 | |
| 1476 | if (copy_to_user((void __user *)arg, &conv, |
| 1477 | sizeof(struct epm_chan_request))) |
| 1478 | return -EFAULT; |
| 1479 | break; |
| 1480 | } |
| 1481 | case EPM_ADC_INIT: |
| 1482 | { |
| 1483 | uint32_t result; |
| 1484 | if (!epm_adc_expander_register) { |
| 1485 | result = epm_adc_i2c_expander_register(); |
| 1486 | if (result) { |
| 1487 | pr_err("Failed i2c register:%d\n", |
| 1488 | result); |
| 1489 | return result; |
| 1490 | } |
| 1491 | epm_adc_expander_register = true; |
| 1492 | } |
| 1493 | |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 1494 | result = epm_adc_hw_init(epm_adc); |
Siddartha Mohanadoss | eb90c92 | 2012-03-27 22:58:06 -0700 | [diff] [blame] | 1495 | |
| 1496 | if (copy_to_user((void __user *)arg, &result, |
| 1497 | sizeof(uint32_t))) |
| 1498 | return -EFAULT; |
| 1499 | break; |
| 1500 | } |
| 1501 | case EPM_ADC_DEINIT: |
| 1502 | { |
| 1503 | uint32_t result; |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 1504 | result = epm_adc_hw_deinit(epm_adc); |
Siddartha Mohanadoss | eb90c92 | 2012-03-27 22:58:06 -0700 | [diff] [blame] | 1505 | |
| 1506 | if (copy_to_user((void __user *)arg, &result, |
| 1507 | sizeof(uint32_t))) |
| 1508 | return -EFAULT; |
| 1509 | break; |
| 1510 | } |
Siddartha Mohanadoss | 3efc4d8 | 2013-06-11 15:34:52 -0700 | [diff] [blame] | 1511 | case EPM_MARKER1_REQUEST: |
| 1512 | { |
| 1513 | struct epm_marker_level marker_init; |
| 1514 | uint32_t result; |
| 1515 | |
| 1516 | if (copy_from_user(&marker_init, (void __user *)arg, |
| 1517 | sizeof(struct epm_marker_level))) |
| 1518 | return -EFAULT; |
| 1519 | |
| 1520 | result = epm_set_marker1(&marker_init); |
| 1521 | |
| 1522 | if (copy_to_user((void __user *)arg, &result, |
| 1523 | sizeof(uint32_t))) |
| 1524 | return -EFAULT; |
| 1525 | break; |
| 1526 | } |
| 1527 | case EPM_MARKER2_REQUEST: |
| 1528 | { |
| 1529 | struct epm_marker_level marker_init; |
| 1530 | uint32_t result; |
| 1531 | |
| 1532 | if (copy_from_user(&marker_init, (void __user *)arg, |
| 1533 | sizeof(struct epm_marker_level))) |
| 1534 | return -EFAULT; |
| 1535 | |
| 1536 | result = epm_set_marker2(&marker_init); |
| 1537 | |
| 1538 | if (copy_to_user((void __user *)arg, &result, |
| 1539 | sizeof(uint32_t))) |
| 1540 | return -EFAULT; |
| 1541 | break; |
| 1542 | } |
| 1543 | case EPM_MARKER1_RELEASE: |
| 1544 | { |
| 1545 | uint32_t result; |
| 1546 | result = epm_marker1_release(); |
| 1547 | |
| 1548 | if (copy_to_user((void __user *)arg, &result, |
| 1549 | sizeof(uint32_t))) |
| 1550 | return -EFAULT; |
| 1551 | break; |
| 1552 | } |
| 1553 | case EPM_MARKER2_RELEASE: |
| 1554 | { |
| 1555 | uint32_t result; |
| 1556 | result = epm_marker2_release(); |
| 1557 | |
| 1558 | if (copy_to_user((void __user *)arg, &result, |
| 1559 | sizeof(uint32_t))) |
| 1560 | return -EFAULT; |
| 1561 | break; |
| 1562 | } |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 1563 | case EPM_PSOC_ADC_INIT: |
| 1564 | { |
| 1565 | struct epm_psoc_init_resp psoc_init; |
| 1566 | int rc; |
| 1567 | |
| 1568 | if (copy_from_user(&psoc_init, (void __user *)arg, |
| 1569 | sizeof(struct epm_psoc_init_resp))) |
| 1570 | return -EFAULT; |
| 1571 | |
| 1572 | psoc_init.cmd = EPM_PSOC_INIT_CMD; |
| 1573 | rc = epm_psoc_init(epm_adc, &psoc_init); |
| 1574 | if (rc) { |
| 1575 | pr_err("PSOC initialization failed\n"); |
| 1576 | return -EINVAL; |
| 1577 | } |
| 1578 | |
Siddartha Mohanadoss | 97242fe | 2013-06-11 16:05:16 -0700 | [diff] [blame] | 1579 | if (!rc) { |
| 1580 | rc = epm_adc_psoc_gpio_init(true); |
| 1581 | if (rc) { |
| 1582 | pr_err("GPIO init failed\n"); |
| 1583 | return -EINVAL; |
| 1584 | } |
| 1585 | } |
| 1586 | |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 1587 | if (copy_to_user((void __user *)arg, &psoc_init, |
| 1588 | sizeof(struct epm_psoc_init_resp))) |
| 1589 | return -EFAULT; |
| 1590 | break; |
| 1591 | } |
Siddartha Mohanadoss | 97242fe | 2013-06-11 16:05:16 -0700 | [diff] [blame] | 1592 | case EPM_PSOC_ADC_DEINIT: |
| 1593 | { |
| 1594 | uint32_t result; |
| 1595 | result = epm_adc_psoc_gpio_init(false); |
| 1596 | |
| 1597 | if (copy_to_user((void __user *)arg, &result, |
| 1598 | sizeof(uint32_t))) |
| 1599 | return -EFAULT; |
| 1600 | break; |
| 1601 | } |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 1602 | case EPM_PSOC_ADC_CHANNEL_ENABLE: |
| 1603 | case EPM_PSOC_ADC_CHANNEL_DISABLE: |
| 1604 | { |
| 1605 | struct epm_psoc_channel_configure psoc_chan_configure; |
| 1606 | int rc; |
| 1607 | |
| 1608 | if (copy_from_user(&psoc_chan_configure, |
| 1609 | (void __user *)arg, |
| 1610 | sizeof(struct epm_psoc_channel_configure))) |
| 1611 | return -EFAULT; |
| 1612 | |
| 1613 | psoc_chan_configure.cmd = |
| 1614 | EPM_PSOC_CHANNEL_ENABLE_DISABLE_CMD; |
| 1615 | rc = epm_psoc_channel_configure(epm_adc, |
| 1616 | &psoc_chan_configure); |
| 1617 | if (rc) { |
| 1618 | pr_err("PSOC channel configure failed\n"); |
| 1619 | return -EINVAL; |
| 1620 | } |
| 1621 | |
| 1622 | if (copy_to_user((void __user *)arg, |
| 1623 | &psoc_chan_configure, |
| 1624 | sizeof(struct epm_psoc_channel_configure))) |
| 1625 | return -EFAULT; |
| 1626 | break; |
| 1627 | } |
| 1628 | case EPM_PSOC_ADC_SET_AVERAGING: |
| 1629 | { |
| 1630 | struct epm_psoc_set_avg psoc_set_avg; |
| 1631 | int rc; |
| 1632 | |
| 1633 | if (copy_from_user(&psoc_set_avg, (void __user *)arg, |
| 1634 | sizeof(struct epm_psoc_set_avg))) |
| 1635 | return -EFAULT; |
| 1636 | |
| 1637 | psoc_set_avg.cmd = EPM_PSOC_SET_AVERAGING_CMD; |
| 1638 | rc = epm_psoc_set_averaging(epm_adc, &psoc_set_avg); |
| 1639 | if (rc) { |
| 1640 | pr_err("PSOC averaging failed\n"); |
| 1641 | return -EINVAL; |
| 1642 | } |
| 1643 | |
| 1644 | if (copy_to_user((void __user *)arg, &psoc_set_avg, |
| 1645 | sizeof(struct epm_psoc_set_avg))) |
| 1646 | return -EFAULT; |
| 1647 | break; |
| 1648 | } |
| 1649 | case EPM_PSOC_ADC_GET_LAST_MEASUREMENT: |
| 1650 | { |
| 1651 | struct epm_psoc_get_data psoc_get_data; |
| 1652 | int rc; |
| 1653 | |
| 1654 | if (copy_from_user(&psoc_get_data, |
| 1655 | (void __user *)arg, |
| 1656 | sizeof(struct epm_psoc_get_data))) |
| 1657 | return -EFAULT; |
| 1658 | |
| 1659 | psoc_get_data.cmd = EPM_PSOC_GET_LAST_MEASUREMENT_CMD; |
| 1660 | rc = epm_psoc_get_data(epm_adc, &psoc_get_data); |
| 1661 | if (rc) { |
| 1662 | pr_err("PSOC last measured data failed\n"); |
| 1663 | return -EINVAL; |
| 1664 | } |
| 1665 | |
Siddartha Mohanadoss | 53c6ebe | 2012-12-06 14:17:30 -0800 | [diff] [blame] | 1666 | psoc_get_data.reading_value = epm_psoc_scale_result( |
Siddartha Mohanadoss | 899173a | 2013-01-29 19:18:50 -0800 | [diff] [blame] | 1667 | psoc_get_data.reading_raw, |
Siddartha Mohanadoss | 53c6ebe | 2012-12-06 14:17:30 -0800 | [diff] [blame] | 1668 | psoc_get_data.chan_num); |
| 1669 | |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 1670 | if (copy_to_user((void __user *)arg, &psoc_get_data, |
| 1671 | sizeof(struct epm_psoc_get_data))) |
| 1672 | return -EFAULT; |
| 1673 | break; |
| 1674 | } |
| 1675 | case EPM_PSOC_ADC_GET_BUFFERED_DATA: |
| 1676 | { |
| 1677 | struct epm_psoc_get_buffered_data psoc_get_data; |
| 1678 | int rc; |
| 1679 | |
| 1680 | if (copy_from_user(&psoc_get_data, |
| 1681 | (void __user *)arg, |
| 1682 | sizeof(struct epm_psoc_get_buffered_data))) |
| 1683 | return -EFAULT; |
| 1684 | |
| 1685 | psoc_get_data.cmd = EPM_PSOC_GET_BUFFERED_DATA_CMD; |
| 1686 | rc = epm_psoc_get_buffered_data(epm_adc, |
| 1687 | &psoc_get_data); |
| 1688 | if (rc) { |
| 1689 | pr_err("PSOC buffered measurement failed\n"); |
| 1690 | return -EINVAL; |
| 1691 | } |
| 1692 | |
| 1693 | if (copy_to_user((void __user *)arg, &psoc_get_data, |
| 1694 | sizeof(struct epm_psoc_get_buffered_data))) |
| 1695 | return -EFAULT; |
| 1696 | break; |
| 1697 | } |
| 1698 | case EPM_PSOC_ADC_GET_SYSTEM_TIMESTAMP: |
Siddartha Mohanadoss | 278815e | 2013-04-22 11:18:17 -0700 | [diff] [blame] | 1699 | { |
| 1700 | struct epm_psoc_system_time_stamp psoc_timestamp; |
| 1701 | int rc; |
| 1702 | |
| 1703 | if (copy_from_user(&psoc_timestamp, |
| 1704 | (void __user *)arg, |
| 1705 | sizeof(struct epm_psoc_system_time_stamp))) |
| 1706 | return -EFAULT; |
| 1707 | |
| 1708 | rc = epm_psoc_get_timestamp(epm_adc, &psoc_timestamp); |
| 1709 | if (rc) { |
| 1710 | pr_err("PSOC get timestamp failed\n"); |
| 1711 | return -EINVAL; |
| 1712 | } |
| 1713 | |
| 1714 | if (copy_to_user((void __user *)arg, &psoc_timestamp, |
| 1715 | sizeof(struct epm_psoc_system_time_stamp))) |
| 1716 | return -EFAULT; |
| 1717 | break; |
| 1718 | } |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 1719 | case EPM_PSOC_ADC_SET_SYSTEM_TIMESTAMP: |
| 1720 | { |
| 1721 | struct epm_psoc_system_time_stamp psoc_timestamp; |
| 1722 | int rc; |
| 1723 | |
| 1724 | if (copy_from_user(&psoc_timestamp, |
| 1725 | (void __user *)arg, |
| 1726 | sizeof(struct epm_psoc_system_time_stamp))) |
| 1727 | return -EFAULT; |
| 1728 | |
Siddartha Mohanadoss | 278815e | 2013-04-22 11:18:17 -0700 | [diff] [blame] | 1729 | rc = epm_psoc_set_timestamp(epm_adc, &psoc_timestamp); |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 1730 | if (rc) { |
Siddartha Mohanadoss | 278815e | 2013-04-22 11:18:17 -0700 | [diff] [blame] | 1731 | pr_err("PSOC set timestamp failed\n"); |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 1732 | return -EINVAL; |
| 1733 | } |
| 1734 | |
| 1735 | if (copy_to_user((void __user *)arg, &psoc_timestamp, |
| 1736 | sizeof(struct epm_psoc_system_time_stamp))) |
| 1737 | return -EFAULT; |
| 1738 | break; |
| 1739 | } |
| 1740 | case EPM_PSOC_ADC_GET_AVERAGE_DATA: |
| 1741 | { |
| 1742 | struct epm_psoc_get_avg_buffered_switch_data |
| 1743 | psoc_get_data; |
| 1744 | int rc; |
| 1745 | |
| 1746 | if (copy_from_user(&psoc_get_data, |
| 1747 | (void __user *)arg, |
| 1748 | sizeof(struct |
| 1749 | epm_psoc_get_avg_buffered_switch_data))) |
| 1750 | return -EFAULT; |
| 1751 | |
| 1752 | psoc_get_data.cmd = EPM_PSOC_GET_AVERAGED_DATA_CMD; |
| 1753 | rc = epm_psoc_get_avg_buffered_switch_data(epm_adc, |
| 1754 | &psoc_get_data); |
| 1755 | if (rc) { |
| 1756 | pr_err("Get averaged buffered data failed\n"); |
| 1757 | return -EINVAL; |
| 1758 | } |
| 1759 | |
| 1760 | if (copy_to_user((void __user *)arg, &psoc_get_data, |
| 1761 | sizeof(struct |
| 1762 | epm_psoc_get_avg_buffered_switch_data))) |
| 1763 | return -EFAULT; |
| 1764 | break; |
| 1765 | } |
| 1766 | case EPM_PSOC_SET_CHANNEL_SWITCH: |
| 1767 | { |
| 1768 | struct epm_psoc_set_channel_switch psoc_channel_switch; |
| 1769 | int rc; |
| 1770 | |
| 1771 | if (copy_from_user(&psoc_channel_switch, |
| 1772 | (void __user *)arg, |
| 1773 | sizeof(struct epm_psoc_set_channel_switch))) |
| 1774 | return -EFAULT; |
| 1775 | |
| 1776 | rc = epm_psoc_set_channel_switch(epm_adc, |
| 1777 | &psoc_channel_switch); |
| 1778 | if (rc) { |
| 1779 | pr_err("PSOC channel switch failed\n"); |
| 1780 | return -EINVAL; |
| 1781 | } |
| 1782 | |
| 1783 | if (copy_to_user((void __user *)arg, |
| 1784 | &psoc_channel_switch, |
| 1785 | sizeof(struct epm_psoc_set_channel_switch))) |
| 1786 | return -EFAULT; |
| 1787 | break; |
| 1788 | } |
| 1789 | case EPM_PSOC_CLEAR_BUFFER: |
| 1790 | { |
| 1791 | int rc; |
| 1792 | rc = epm_psoc_clear_buffer(epm_adc); |
| 1793 | if (rc) { |
| 1794 | pr_err("PSOC clear buffer failed\n"); |
| 1795 | return -EINVAL; |
| 1796 | } |
| 1797 | |
| 1798 | if (copy_to_user((void __user *)arg, &rc, |
| 1799 | sizeof(uint32_t))) |
| 1800 | return -EFAULT; |
| 1801 | break; |
| 1802 | } |
| 1803 | case EPM_PSOC_ADC_SET_VADC_REFERENCE: |
| 1804 | { |
| 1805 | struct epm_psoc_set_vadc psoc_set_vadc; |
| 1806 | int rc; |
| 1807 | |
| 1808 | if (copy_from_user(&psoc_set_vadc, |
| 1809 | (void __user *)arg, |
| 1810 | sizeof(struct epm_psoc_set_vadc))) |
| 1811 | return -EFAULT; |
| 1812 | |
| 1813 | rc = epm_psoc_set_vadc(epm_adc, &psoc_set_vadc); |
| 1814 | if (rc) { |
| 1815 | pr_err("PSOC set VADC failed\n"); |
| 1816 | return -EINVAL; |
| 1817 | } |
| 1818 | |
| 1819 | if (copy_to_user((void __user *)arg, &psoc_set_vadc, |
| 1820 | sizeof(struct epm_psoc_set_vadc))) |
| 1821 | return -EFAULT; |
| 1822 | break; |
| 1823 | } |
Siddartha Mohanadoss | d34827d | 2013-06-12 09:57:30 -0700 | [diff] [blame] | 1824 | case EPM_PSOC_GPIO_BUFFER_REQUEST: |
| 1825 | { |
| 1826 | struct epm_gpio_buffer_request gpio_request; |
| 1827 | int rc; |
| 1828 | |
| 1829 | if (copy_from_user(&gpio_request, |
| 1830 | (void __user *)arg, |
| 1831 | sizeof(struct epm_gpio_buffer_request))) |
| 1832 | return -EFAULT; |
| 1833 | |
| 1834 | rc = epm_psoc_gpio_buffer_request_configure(epm_adc, |
| 1835 | &gpio_request); |
| 1836 | if (rc) { |
| 1837 | pr_err("PSOC buffer request failed\n"); |
| 1838 | return -EINVAL; |
| 1839 | } |
| 1840 | |
| 1841 | if (copy_to_user((void __user *)arg, &gpio_request, |
| 1842 | sizeof(struct epm_gpio_buffer_request))) |
| 1843 | return -EFAULT; |
| 1844 | break; |
| 1845 | } |
| 1846 | case EPM_PSOC_GET_GPIO_BUFFER_DATA: |
| 1847 | { |
| 1848 | struct epm_get_gpio_buffer_resp gpio_resp_pkt; |
| 1849 | int rc; |
| 1850 | |
| 1851 | if (copy_from_user(&gpio_resp_pkt, |
| 1852 | (void __user *)arg, |
| 1853 | sizeof(struct epm_get_gpio_buffer_resp))) |
| 1854 | return -EFAULT; |
| 1855 | |
| 1856 | rc = epm_psoc_get_gpio_buffer_data(epm_adc, |
| 1857 | &gpio_resp_pkt); |
| 1858 | if (rc) { |
| 1859 | pr_err("PSOC get buffer data failed\n"); |
| 1860 | return -EINVAL; |
| 1861 | } |
| 1862 | |
| 1863 | if (copy_to_user((void __user *)arg, &gpio_resp_pkt, |
| 1864 | sizeof(struct epm_get_gpio_buffer_resp))) |
| 1865 | return -EFAULT; |
| 1866 | break; |
| 1867 | } |
Siddartha Mohanadoss | 46aff91 | 2013-07-10 18:53:48 -0700 | [diff] [blame] | 1868 | case EPM_PSOC_PAUSE_CONVERSION_REQUEST: |
| 1869 | { |
| 1870 | uint32_t result; |
| 1871 | result = epm_psoc_pause_conversion(epm_adc); |
| 1872 | |
| 1873 | if (copy_to_user((void __user *)arg, &result, |
| 1874 | sizeof(uint32_t))) |
| 1875 | return -EFAULT; |
| 1876 | break; |
| 1877 | } |
| 1878 | case EPM_PSOC_UNPAUSE_CONVERSION_REQUEST: |
| 1879 | { |
| 1880 | uint32_t result; |
| 1881 | result = epm_psoc_unpause_conversion(epm_adc); |
| 1882 | |
| 1883 | if (copy_to_user((void __user *)arg, &result, |
| 1884 | sizeof(uint32_t))) |
| 1885 | return -EFAULT; |
| 1886 | break; |
| 1887 | } |
Siddartha Mohanadoss | eb90c92 | 2012-03-27 22:58:06 -0700 | [diff] [blame] | 1888 | default: |
| 1889 | return -EINVAL; |
| 1890 | } |
| 1891 | |
| 1892 | return 0; |
| 1893 | } |
| 1894 | |
| 1895 | const struct file_operations epm_adc_fops = { |
| 1896 | .unlocked_ioctl = epm_adc_ioctl, |
| 1897 | }; |
| 1898 | |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 1899 | static ssize_t epm_adc_psoc_show_in(struct device *dev, |
| 1900 | struct device_attribute *devattr, char *buf) |
| 1901 | { |
| 1902 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 1903 | struct epm_adc_drv *epm_adc = epm_adc_drv; |
| 1904 | struct epm_psoc_init_resp init_resp; |
| 1905 | struct epm_psoc_channel_configure psoc_chan_configure; |
| 1906 | struct epm_psoc_get_data psoc_get_meas; |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 1907 | int rc = 0; |
| 1908 | |
| 1909 | rc = epm_adc_psoc_gpio_init(true); |
| 1910 | if (rc) { |
| 1911 | pr_err("GPIO init failed\n"); |
| 1912 | return 0; |
| 1913 | } |
| 1914 | usleep_range(EPM_GLOBAL_ENABLE_MIN_DELAY, |
| 1915 | EPM_GLOBAL_ENABLE_MAX_DELAY); |
| 1916 | |
| 1917 | init_resp.cmd = EPM_PSOC_INIT_CMD; |
| 1918 | rc = epm_psoc_init(epm_adc, &init_resp); |
| 1919 | if (rc) { |
Siddartha Mohanadoss | 6e77b77 | 2012-09-19 21:58:13 -0700 | [diff] [blame] | 1920 | pr_err("PSOC init failed %d\n", rc); |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 1921 | return 0; |
| 1922 | } |
| 1923 | |
Siddartha Mohanadoss | 6e77b77 | 2012-09-19 21:58:13 -0700 | [diff] [blame] | 1924 | |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 1925 | psoc_chan_configure.channel_num = (1 << attr->index); |
| 1926 | psoc_chan_configure.cmd = EPM_PSOC_CHANNEL_ENABLE_DISABLE_CMD; |
| 1927 | rc = epm_psoc_channel_configure(epm_adc, &psoc_chan_configure); |
| 1928 | if (rc) { |
Siddartha Mohanadoss | 6e77b77 | 2012-09-19 21:58:13 -0700 | [diff] [blame] | 1929 | pr_err("PSOC channel configure failed\n"); |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 1930 | return 0; |
| 1931 | } |
| 1932 | |
| 1933 | usleep_range(EPM_GLOBAL_ENABLE_MIN_DELAY, |
| 1934 | EPM_GLOBAL_ENABLE_MAX_DELAY); |
| 1935 | |
| 1936 | psoc_get_meas.cmd = EPM_PSOC_GET_LAST_MEASUREMENT_CMD; |
| 1937 | psoc_get_meas.dev_num = 0; |
| 1938 | psoc_get_meas.chan_num = attr->index; |
| 1939 | rc = epm_psoc_get_data(epm_adc, &psoc_get_meas); |
| 1940 | if (rc) { |
Siddartha Mohanadoss | 6e77b77 | 2012-09-19 21:58:13 -0700 | [diff] [blame] | 1941 | pr_err("PSOC get data failed\n"); |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 1942 | return 0; |
| 1943 | } |
| 1944 | |
Siddartha Mohanadoss | 6e77b77 | 2012-09-19 21:58:13 -0700 | [diff] [blame] | 1945 | psoc_get_meas.reading_value = epm_psoc_scale_result( |
| 1946 | psoc_get_meas.reading_value, |
| 1947 | attr->index); |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 1948 | |
| 1949 | rc = epm_adc_psoc_gpio_init(false); |
| 1950 | if (rc) { |
| 1951 | pr_err("GPIO de-init failed\n"); |
| 1952 | return 0; |
| 1953 | } |
| 1954 | |
| 1955 | return snprintf(buf, 16, "Result: %d\n", psoc_get_meas.reading_value); |
| 1956 | } |
| 1957 | |
| 1958 | static struct sensor_device_attribute epm_adc_psoc_in_attrs[] = { |
Siddartha Mohanadoss | 37e1c37 | 2012-12-06 14:19:11 -0800 | [diff] [blame] | 1959 | SENSOR_ATTR(psoc0_chan0, S_IRUGO, epm_adc_psoc_show_in, NULL, 0), |
| 1960 | SENSOR_ATTR(psoc0_chan1, S_IRUGO, epm_adc_psoc_show_in, NULL, 1), |
| 1961 | SENSOR_ATTR(psoc0_chan2, S_IRUGO, epm_adc_psoc_show_in, NULL, 2), |
| 1962 | SENSOR_ATTR(psoc0_chan3, S_IRUGO, epm_adc_psoc_show_in, NULL, 3), |
| 1963 | SENSOR_ATTR(psoc0_chan4, S_IRUGO, epm_adc_psoc_show_in, NULL, 4), |
| 1964 | SENSOR_ATTR(psoc0_chan5, S_IRUGO, epm_adc_psoc_show_in, NULL, 5), |
| 1965 | SENSOR_ATTR(psoc0_chan6, S_IRUGO, epm_adc_psoc_show_in, NULL, 6), |
| 1966 | SENSOR_ATTR(psoc0_chan7, S_IRUGO, epm_adc_psoc_show_in, NULL, 7), |
| 1967 | SENSOR_ATTR(psoc0_chan8, S_IRUGO, epm_adc_psoc_show_in, NULL, 8), |
| 1968 | SENSOR_ATTR(psoc0_chan9, S_IRUGO, epm_adc_psoc_show_in, NULL, 9), |
| 1969 | SENSOR_ATTR(psoc0_chan10, S_IRUGO, epm_adc_psoc_show_in, NULL, 10), |
| 1970 | SENSOR_ATTR(psoc0_chan11, S_IRUGO, epm_adc_psoc_show_in, NULL, 11), |
| 1971 | SENSOR_ATTR(psoc0_chan12, S_IRUGO, epm_adc_psoc_show_in, NULL, 12), |
| 1972 | SENSOR_ATTR(psoc0_chan13, S_IRUGO, epm_adc_psoc_show_in, NULL, 13), |
| 1973 | SENSOR_ATTR(psoc0_chan14, S_IRUGO, epm_adc_psoc_show_in, NULL, 14), |
| 1974 | SENSOR_ATTR(psoc0_chan15, S_IRUGO, epm_adc_psoc_show_in, NULL, 15), |
| 1975 | SENSOR_ATTR(psoc0_chan16, S_IRUGO, epm_adc_psoc_show_in, NULL, 16), |
| 1976 | SENSOR_ATTR(psoc0_chan17, S_IRUGO, epm_adc_psoc_show_in, NULL, 17), |
| 1977 | SENSOR_ATTR(psoc0_chan18, S_IRUGO, epm_adc_psoc_show_in, NULL, 18), |
| 1978 | SENSOR_ATTR(psoc0_chan19, S_IRUGO, epm_adc_psoc_show_in, NULL, 19), |
| 1979 | SENSOR_ATTR(psoc0_chan20, S_IRUGO, epm_adc_psoc_show_in, NULL, 20), |
| 1980 | SENSOR_ATTR(psoc0_chan21, S_IRUGO, epm_adc_psoc_show_in, NULL, 21), |
| 1981 | SENSOR_ATTR(psoc0_chan22, S_IRUGO, epm_adc_psoc_show_in, NULL, 22), |
| 1982 | SENSOR_ATTR(psoc0_chan23, S_IRUGO, epm_adc_psoc_show_in, NULL, 23), |
| 1983 | SENSOR_ATTR(psoc0_chan24, S_IRUGO, epm_adc_psoc_show_in, NULL, 24), |
| 1984 | SENSOR_ATTR(psoc0_chan25, S_IRUGO, epm_adc_psoc_show_in, NULL, 25), |
| 1985 | SENSOR_ATTR(psoc0_chan26, S_IRUGO, epm_adc_psoc_show_in, NULL, 26), |
| 1986 | SENSOR_ATTR(psoc0_chan27, S_IRUGO, epm_adc_psoc_show_in, NULL, 27), |
| 1987 | SENSOR_ATTR(psoc0_chan28, S_IRUGO, epm_adc_psoc_show_in, NULL, 28), |
| 1988 | SENSOR_ATTR(psoc0_chan29, S_IRUGO, epm_adc_psoc_show_in, NULL, 29), |
| 1989 | SENSOR_ATTR(psoc0_chan30, S_IRUGO, epm_adc_psoc_show_in, NULL, 30), |
| 1990 | SENSOR_ATTR(psoc0_chan31, S_IRUGO, epm_adc_psoc_show_in, NULL, 31), |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 1991 | }; |
| 1992 | |
| 1993 | static int __devinit epm_adc_psoc_init_hwmon(struct spi_device *spi, |
| 1994 | struct epm_adc_drv *epm_adc) |
| 1995 | { |
Siddartha Mohanadoss | 6e77b77 | 2012-09-19 21:58:13 -0700 | [diff] [blame] | 1996 | int i, rc, num_chans = 31; |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 1997 | |
| 1998 | for (i = 0; i < num_chans; i++) { |
| 1999 | rc = device_create_file(&spi->dev, |
| 2000 | &epm_adc_psoc_in_attrs[i].dev_attr); |
| 2001 | if (rc) { |
| 2002 | dev_err(&spi->dev, "device_create_file failed\n"); |
| 2003 | return rc; |
| 2004 | } |
| 2005 | } |
| 2006 | |
| 2007 | return 0; |
| 2008 | } |
| 2009 | |
| 2010 | static int get_device_tree_data(struct spi_device *spi) |
| 2011 | { |
| 2012 | const struct device_node *node = spi->dev.of_node; |
| 2013 | struct epm_adc_drv *epm_adc; |
Siddartha Mohanadoss | 6e77b77 | 2012-09-19 21:58:13 -0700 | [diff] [blame] | 2014 | u32 *epm_ch_gain, *epm_ch_rsense; |
| 2015 | u32 rc = 0, epm_num_channels, i, channel_mask; |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 2016 | |
| 2017 | if (!node) |
| 2018 | return -EINVAL; |
| 2019 | |
| 2020 | rc = of_property_read_u32(node, |
| 2021 | "qcom,channels", &epm_num_channels); |
| 2022 | if (rc) { |
| 2023 | dev_err(&spi->dev, "missing channel numbers\n"); |
| 2024 | return -ENODEV; |
| 2025 | } |
| 2026 | |
| 2027 | epm_ch_gain = devm_kzalloc(&spi->dev, |
Siddartha Mohanadoss | 6e77b77 | 2012-09-19 21:58:13 -0700 | [diff] [blame] | 2028 | epm_num_channels * sizeof(u32), GFP_KERNEL); |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 2029 | if (!epm_ch_gain) { |
| 2030 | dev_err(&spi->dev, "cannot allocate gain\n"); |
| 2031 | return -ENOMEM; |
| 2032 | } |
| 2033 | |
| 2034 | epm_ch_rsense = devm_kzalloc(&spi->dev, |
Siddartha Mohanadoss | 6e77b77 | 2012-09-19 21:58:13 -0700 | [diff] [blame] | 2035 | epm_num_channels * sizeof(u32), GFP_KERNEL); |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 2036 | if (!epm_ch_rsense) { |
| 2037 | dev_err(&spi->dev, "cannot allocate rsense\n"); |
| 2038 | return -ENOMEM; |
| 2039 | } |
| 2040 | |
| 2041 | rc = of_property_read_u32_array(node, |
| 2042 | "qcom,gain", epm_ch_gain, epm_num_channels); |
| 2043 | if (rc) { |
| 2044 | dev_err(&spi->dev, "invalid gain property:%d\n", rc); |
| 2045 | return rc; |
| 2046 | } |
| 2047 | |
| 2048 | rc = of_property_read_u32_array(node, |
| 2049 | "qcom,rsense", epm_ch_rsense, epm_num_channels); |
| 2050 | if (rc) { |
| 2051 | dev_err(&spi->dev, "invalid rsense property:%d\n", rc); |
| 2052 | return rc; |
| 2053 | } |
| 2054 | |
Siddartha Mohanadoss | 6e77b77 | 2012-09-19 21:58:13 -0700 | [diff] [blame] | 2055 | rc = of_property_read_u32(node, |
| 2056 | "qcom,channel-type", &channel_mask); |
| 2057 | if (rc) { |
| 2058 | dev_err(&spi->dev, "missing channel mask\n"); |
| 2059 | return -ENODEV; |
| 2060 | } |
| 2061 | |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 2062 | epm_adc = devm_kzalloc(&spi->dev, |
| 2063 | sizeof(struct epm_adc_drv) + |
| 2064 | (epm_num_channels * |
| 2065 | sizeof(struct epm_chan_properties)), |
| 2066 | GFP_KERNEL); |
| 2067 | if (!epm_adc) { |
| 2068 | dev_err(&spi->dev, "Unable to allocate memory\n"); |
| 2069 | return -ENOMEM; |
| 2070 | } |
| 2071 | |
| 2072 | for (i = 0; i < epm_num_channels; i++) { |
| 2073 | epm_adc->epm_psoc_ch_prop[i].resistorvalue = |
| 2074 | epm_ch_rsense[i]; |
| 2075 | epm_adc->epm_psoc_ch_prop[i].gain = |
| 2076 | epm_ch_gain[i]; |
| 2077 | } |
| 2078 | |
Siddartha Mohanadoss | 6e77b77 | 2012-09-19 21:58:13 -0700 | [diff] [blame] | 2079 | epm_adc->channel_mask = channel_mask; |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 2080 | epm_adc_drv = epm_adc; |
| 2081 | |
| 2082 | return 0; |
| 2083 | } |
| 2084 | |
| 2085 | static int __devinit epm_adc_psoc_spi_probe(struct spi_device *spi) |
| 2086 | { |
Siddartha Mohanadoss | 6e77b77 | 2012-09-19 21:58:13 -0700 | [diff] [blame] | 2087 | |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 2088 | struct epm_adc_drv *epm_adc; |
| 2089 | struct device_node *node = spi->dev.of_node; |
| 2090 | int rc = 0; |
| 2091 | |
Siddartha Mohanadoss | 6e77b77 | 2012-09-19 21:58:13 -0700 | [diff] [blame] | 2092 | if (node) { |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 2093 | rc = get_device_tree_data(spi); |
Siddartha Mohanadoss | 6e77b77 | 2012-09-19 21:58:13 -0700 | [diff] [blame] | 2094 | if (rc) |
| 2095 | return rc; |
| 2096 | } else { |
Siddartha Mohanadoss | de90ed4 | 2012-09-18 16:57:50 -0700 | [diff] [blame] | 2097 | epm_adc = epm_adc_drv; |
| 2098 | epm_adc_drv->epm_spi_client = spi; |
| 2099 | epm_adc_drv->epm_spi_client->bits_per_word = |
| 2100 | EPM_ADC_ADS_SPI_BITS_PER_WORD; |
| 2101 | return rc; |
| 2102 | } |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 2103 | |
| 2104 | epm_adc = epm_adc_drv; |
| 2105 | epm_adc->misc.name = EPM_ADC_DRIVER_NAME; |
| 2106 | epm_adc->misc.minor = MISC_DYNAMIC_MINOR; |
Siddartha Mohanadoss | 5e8df03 | 2012-11-12 17:54:03 -0800 | [diff] [blame] | 2107 | |
| 2108 | if (node) { |
| 2109 | epm_adc->misc.fops = &epm_adc_fops; |
| 2110 | if (misc_register(&epm_adc->misc)) { |
| 2111 | pr_err("Unable to register misc device!\n"); |
| 2112 | return -EFAULT; |
| 2113 | } |
| 2114 | } |
| 2115 | |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 2116 | epm_adc_drv->epm_spi_client = spi; |
| 2117 | epm_adc_drv->epm_spi_client->bits_per_word = |
| 2118 | EPM_ADC_ADS_SPI_BITS_PER_WORD; |
| 2119 | rc = epm_adc_psoc_init_hwmon(spi, epm_adc); |
| 2120 | if (rc) { |
| 2121 | dev_err(&spi->dev, "msm_adc_dev_init failed\n"); |
| 2122 | return rc; |
| 2123 | } |
| 2124 | |
| 2125 | epm_adc->hwmon = hwmon_device_register(&spi->dev); |
| 2126 | if (IS_ERR(epm_adc->hwmon)) { |
| 2127 | dev_err(&spi->dev, "hwmon_device_register failed\n"); |
| 2128 | return rc; |
| 2129 | } |
| 2130 | |
| 2131 | mutex_init(&epm_adc->conv_lock); |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 2132 | return rc; |
| 2133 | } |
| 2134 | |
| 2135 | static int __devexit epm_adc_psoc_spi_remove(struct spi_device *spi) |
| 2136 | { |
| 2137 | epm_adc_drv->epm_spi_client = NULL; |
| 2138 | return 0; |
| 2139 | } |
| 2140 | |
| 2141 | static const struct of_device_id epm_adc_psoc_match_table[] = { |
Siddartha Mohanadoss | 6eadd58 | 2012-10-25 15:29:22 -0700 | [diff] [blame] | 2142 | { .compatible = "cy,epm-adc-cy8c5568lti-114", |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 2143 | }, |
| 2144 | {} |
| 2145 | }; |
| 2146 | |
| 2147 | static struct spi_driver epm_spi_driver = { |
| 2148 | .probe = epm_adc_psoc_spi_probe, |
| 2149 | .remove = __devexit_p(epm_adc_psoc_spi_remove), |
| 2150 | .driver = { |
| 2151 | .name = EPM_ADC_DRIVER_NAME, |
| 2152 | .of_match_table = epm_adc_psoc_match_table, |
| 2153 | }, |
| 2154 | }; |
| 2155 | |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 2156 | static ssize_t epm_adc_show_in(struct device *dev, |
| 2157 | struct device_attribute *devattr, char *buf) |
| 2158 | { |
| 2159 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 2160 | struct epm_adc_drv *epm_adc = dev_get_drvdata(dev); |
| 2161 | struct epm_adc_platform_data *pdata = epm_adc->pdev->dev.platform_data; |
| 2162 | struct epm_chan_request conv; |
| 2163 | int rc = 0; |
| 2164 | |
| 2165 | conv.device_idx = attr->index / pdata->chan_per_adc; |
| 2166 | conv.channel_idx = attr->index % pdata->chan_per_adc; |
| 2167 | conv.physical = 0; |
Siddartha Mohanadoss | 6e77b77 | 2012-09-19 21:58:13 -0700 | [diff] [blame] | 2168 | |
Siddartha Mohanadoss | eb90c92 | 2012-03-27 22:58:06 -0700 | [diff] [blame] | 2169 | if (!epm_adc_expander_register) { |
| 2170 | rc = epm_adc_i2c_expander_register(); |
| 2171 | if (rc) { |
| 2172 | pr_err("I2C expander register failed:%d\n", rc); |
| 2173 | return rc; |
| 2174 | } |
| 2175 | epm_adc_expander_register = true; |
| 2176 | } |
| 2177 | |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 2178 | rc = epm_adc_hw_init(epm_adc); |
| 2179 | if (rc) { |
| 2180 | pr_err("%s: epm_adc_hw_init() failed, rc = %d", |
| 2181 | __func__, rc); |
| 2182 | return 0; |
| 2183 | } |
| 2184 | |
| 2185 | rc = epm_adc_blocking_conversion(epm_adc, &conv); |
| 2186 | if (rc) { |
| 2187 | pr_err("%s: epm_adc_blocking_conversion() failed, rc = %d\n", |
| 2188 | __func__, rc); |
| 2189 | return 0; |
| 2190 | } |
Siddartha Mohanadoss | 6e77b77 | 2012-09-19 21:58:13 -0700 | [diff] [blame] | 2191 | |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 2192 | rc = epm_adc_hw_deinit(epm_adc); |
| 2193 | if (rc) { |
| 2194 | pr_err("%s: epm_adc_hw_deinit() failed, rc = %d", |
| 2195 | __func__, rc); |
| 2196 | return 0; |
| 2197 | } |
| 2198 | |
| 2199 | return snprintf(buf, 16, "Result: %d\n", conv.physical); |
| 2200 | } |
| 2201 | |
Stephen Boyd | aeaf2ad | 2012-07-03 14:33:25 -0700 | [diff] [blame] | 2202 | static struct sensor_device_attribute epm_adc_in_attrs[] = { |
| 2203 | SENSOR_ATTR(ads0_chan0, S_IRUGO, epm_adc_show_in, NULL, 0), |
| 2204 | SENSOR_ATTR(ads0_chan1, S_IRUGO, epm_adc_show_in, NULL, 1), |
| 2205 | SENSOR_ATTR(ads0_chan2, S_IRUGO, epm_adc_show_in, NULL, 2), |
| 2206 | SENSOR_ATTR(ads0_chan3, S_IRUGO, epm_adc_show_in, NULL, 3), |
| 2207 | SENSOR_ATTR(ads0_chan4, S_IRUGO, epm_adc_show_in, NULL, 4), |
| 2208 | SENSOR_ATTR(ads0_chan5, S_IRUGO, epm_adc_show_in, NULL, 5), |
| 2209 | SENSOR_ATTR(ads0_chan6, S_IRUGO, epm_adc_show_in, NULL, 6), |
| 2210 | SENSOR_ATTR(ads0_chan7, S_IRUGO, epm_adc_show_in, NULL, 7), |
| 2211 | SENSOR_ATTR(ads0_chan8, S_IRUGO, epm_adc_show_in, NULL, 8), |
| 2212 | SENSOR_ATTR(ads0_chan9, S_IRUGO, epm_adc_show_in, NULL, 9), |
| 2213 | SENSOR_ATTR(ads0_chan10, S_IRUGO, epm_adc_show_in, NULL, 10), |
| 2214 | SENSOR_ATTR(ads0_chan11, S_IRUGO, epm_adc_show_in, NULL, 11), |
| 2215 | SENSOR_ATTR(ads0_chan12, S_IRUGO, epm_adc_show_in, NULL, 12), |
| 2216 | SENSOR_ATTR(ads0_chan13, S_IRUGO, epm_adc_show_in, NULL, 13), |
| 2217 | SENSOR_ATTR(ads0_chan14, S_IRUGO, epm_adc_show_in, NULL, 14), |
| 2218 | SENSOR_ATTR(ads0_chan15, S_IRUGO, epm_adc_show_in, NULL, 15), |
| 2219 | SENSOR_ATTR(ads1_chan0, S_IRUGO, epm_adc_show_in, NULL, 16), |
| 2220 | SENSOR_ATTR(ads1_chan1, S_IRUGO, epm_adc_show_in, NULL, 17), |
| 2221 | SENSOR_ATTR(ads1_chan2, S_IRUGO, epm_adc_show_in, NULL, 18), |
| 2222 | SENSOR_ATTR(ads1_chan3, S_IRUGO, epm_adc_show_in, NULL, 19), |
| 2223 | SENSOR_ATTR(ads1_chan4, S_IRUGO, epm_adc_show_in, NULL, 20), |
| 2224 | SENSOR_ATTR(ads1_chan5, S_IRUGO, epm_adc_show_in, NULL, 21), |
| 2225 | SENSOR_ATTR(ads1_chan6, S_IRUGO, epm_adc_show_in, NULL, 22), |
| 2226 | SENSOR_ATTR(ads1_chan7, S_IRUGO, epm_adc_show_in, NULL, 23), |
| 2227 | SENSOR_ATTR(ads1_chan8, S_IRUGO, epm_adc_show_in, NULL, 24), |
| 2228 | SENSOR_ATTR(ads1_chan9, S_IRUGO, epm_adc_show_in, NULL, 25), |
| 2229 | SENSOR_ATTR(ads1_chan10, S_IRUGO, epm_adc_show_in, NULL, 26), |
| 2230 | SENSOR_ATTR(ads1_chan11, S_IRUGO, epm_adc_show_in, NULL, 27), |
| 2231 | SENSOR_ATTR(ads1_chan12, S_IRUGO, epm_adc_show_in, NULL, 28), |
| 2232 | SENSOR_ATTR(ads1_chan13, S_IRUGO, epm_adc_show_in, NULL, 29), |
| 2233 | SENSOR_ATTR(ads1_chan14, S_IRUGO, epm_adc_show_in, NULL, 30), |
| 2234 | SENSOR_ATTR(ads1_chan15, S_IRUGO, epm_adc_show_in, NULL, 31), |
| 2235 | }; |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 2236 | |
| 2237 | static int __devinit epm_adc_init_hwmon(struct platform_device *pdev, |
| 2238 | struct epm_adc_drv *epm_adc) |
| 2239 | { |
| 2240 | struct epm_adc_platform_data *pdata = pdev->dev.platform_data; |
Stephen Boyd | aeaf2ad | 2012-07-03 14:33:25 -0700 | [diff] [blame] | 2241 | int i, rc, num_chans = pdata->num_channels; |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 2242 | |
Stephen Boyd | aeaf2ad | 2012-07-03 14:33:25 -0700 | [diff] [blame] | 2243 | for (i = 0; i < num_chans; i++) { |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 2244 | rc = device_create_file(&pdev->dev, |
Stephen Boyd | aeaf2ad | 2012-07-03 14:33:25 -0700 | [diff] [blame] | 2245 | &epm_adc_in_attrs[i].dev_attr); |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 2246 | if (rc) { |
| 2247 | dev_err(&pdev->dev, "device_create_file failed\n"); |
| 2248 | return rc; |
| 2249 | } |
| 2250 | } |
| 2251 | |
Stephen Boyd | aeaf2ad | 2012-07-03 14:33:25 -0700 | [diff] [blame] | 2252 | return 0; |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 2253 | } |
| 2254 | |
Stephen Boyd | 58701e8 | 2012-04-25 11:48:28 -0700 | [diff] [blame] | 2255 | static int __devinit epm_adc_probe(struct platform_device *pdev) |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 2256 | { |
| 2257 | struct epm_adc_drv *epm_adc; |
| 2258 | struct epm_adc_platform_data *pdata = pdev->dev.platform_data; |
| 2259 | int rc = 0; |
| 2260 | |
| 2261 | if (!pdata) { |
| 2262 | dev_err(&pdev->dev, "no platform data?\n"); |
| 2263 | return -EINVAL; |
| 2264 | } |
| 2265 | |
| 2266 | epm_adc = kzalloc(sizeof(struct epm_adc_drv), GFP_KERNEL); |
| 2267 | if (!epm_adc) { |
| 2268 | dev_err(&pdev->dev, "Unable to allocate memory\n"); |
| 2269 | return -ENOMEM; |
| 2270 | } |
| 2271 | |
| 2272 | platform_set_drvdata(pdev, epm_adc); |
| 2273 | epm_adc_drv = epm_adc; |
| 2274 | epm_adc->pdev = pdev; |
| 2275 | |
Siddartha Mohanadoss | eb90c92 | 2012-03-27 22:58:06 -0700 | [diff] [blame] | 2276 | epm_adc->misc.name = EPM_ADC_DRIVER_NAME; |
| 2277 | epm_adc->misc.minor = MISC_DYNAMIC_MINOR; |
| 2278 | epm_adc->misc.fops = &epm_adc_fops; |
| 2279 | |
| 2280 | if (misc_register(&epm_adc->misc)) { |
| 2281 | dev_err(&pdev->dev, "Unable to register misc device!\n"); |
| 2282 | return -EFAULT; |
| 2283 | } |
| 2284 | |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 2285 | rc = epm_adc_init_hwmon(pdev, epm_adc); |
| 2286 | if (rc) { |
| 2287 | dev_err(&pdev->dev, "msm_adc_dev_init failed\n"); |
Siddartha Mohanadoss | eb90c92 | 2012-03-27 22:58:06 -0700 | [diff] [blame] | 2288 | misc_deregister(&epm_adc->misc); |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 2289 | return rc; |
| 2290 | } |
| 2291 | |
| 2292 | epm_adc->hwmon = hwmon_device_register(&pdev->dev); |
| 2293 | if (IS_ERR(epm_adc->hwmon)) { |
| 2294 | dev_err(&pdev->dev, "hwmon_device_register failed\n"); |
Siddartha Mohanadoss | eb90c92 | 2012-03-27 22:58:06 -0700 | [diff] [blame] | 2295 | misc_deregister(&epm_adc->misc); |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 2296 | rc = PTR_ERR(epm_adc->hwmon); |
| 2297 | return rc; |
| 2298 | } |
| 2299 | |
| 2300 | mutex_init(&epm_adc->conv_lock); |
| 2301 | epm_i2c_info = &pdata->epm_i2c_board_info; |
| 2302 | epm_adc->bus_id = pdata->bus_id; |
| 2303 | epm_gpio_expander_base_addr = pdata->gpio_expander_base_addr; |
Siddartha Mohanadoss | eb90c92 | 2012-03-27 22:58:06 -0700 | [diff] [blame] | 2304 | epm_adc_expander_register = false; |
Siddartha Mohanadoss | efddea4 | 2012-09-04 08:23:43 -0700 | [diff] [blame] | 2305 | |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 2306 | return rc; |
| 2307 | } |
| 2308 | |
| 2309 | static int __devexit epm_adc_remove(struct platform_device *pdev) |
| 2310 | { |
| 2311 | struct epm_adc_drv *epm_adc = platform_get_drvdata(pdev); |
| 2312 | struct epm_adc_platform_data *pdata = pdev->dev.platform_data; |
| 2313 | int num_chans = pdata->num_channels; |
| 2314 | int i = 0; |
| 2315 | |
Stephen Boyd | aeaf2ad | 2012-07-03 14:33:25 -0700 | [diff] [blame] | 2316 | for (i = 0; i < num_chans; i++) |
| 2317 | device_remove_file(&pdev->dev, &epm_adc_in_attrs[i].dev_attr); |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 2318 | hwmon_device_unregister(epm_adc->hwmon); |
Siddartha Mohanadoss | eb90c92 | 2012-03-27 22:58:06 -0700 | [diff] [blame] | 2319 | misc_deregister(&epm_adc->misc); |
Siddartha Mohanadoss | 53c16f9 | 2011-10-17 15:57:15 -0700 | [diff] [blame] | 2320 | epm_adc = NULL; |
| 2321 | |
| 2322 | return 0; |
| 2323 | } |
| 2324 | |
| 2325 | static struct platform_driver epm_adc_driver = { |
| 2326 | .probe = epm_adc_probe, |
| 2327 | .remove = __devexit_p(epm_adc_remove), |
| 2328 | .driver = { |
| 2329 | .name = EPM_ADC_DRIVER_NAME, |
| 2330 | .owner = THIS_MODULE, |
| 2331 | }, |
| 2332 | }; |
| 2333 | |
| 2334 | static int __init epm_adc_init(void) |
| 2335 | { |
| 2336 | int ret = 0; |
| 2337 | |
| 2338 | ret = platform_driver_register(&epm_adc_driver); |
| 2339 | if (ret) { |
| 2340 | pr_err("%s: driver register failed, rc=%d\n", __func__, ret); |
| 2341 | return ret; |
| 2342 | } |
| 2343 | |
| 2344 | ret = spi_register_driver(&epm_spi_driver); |
| 2345 | if (ret) |
| 2346 | pr_err("%s: spi register failed: rc=%d\n", __func__, ret); |
| 2347 | |
| 2348 | return ret; |
| 2349 | } |
| 2350 | |
| 2351 | static void __exit epm_adc_exit(void) |
| 2352 | { |
| 2353 | spi_unregister_driver(&epm_spi_driver); |
| 2354 | platform_driver_unregister(&epm_adc_driver); |
| 2355 | } |
| 2356 | |
| 2357 | module_init(epm_adc_init); |
| 2358 | module_exit(epm_adc_exit); |
| 2359 | |
| 2360 | MODULE_DESCRIPTION("EPM ADC Driver"); |
| 2361 | MODULE_ALIAS("platform:epm_adc"); |
| 2362 | MODULE_LICENSE("GPL v2"); |