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