David Collins | 8836530 | 2012-07-17 09:34:57 -0700 | [diff] [blame] | 1 | /* |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 2 | * Copyright (c) 2011-2013, The Linux Foundation. All rights reserved. |
David Collins | 8836530 | 2012-07-17 09:34:57 -0700 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 and |
| 6 | * only version 2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | |
| 14 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
| 15 | |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/delay.h> |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/string.h> |
| 20 | #include <linux/interrupt.h> |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/spmi.h> |
| 24 | #include <linux/of.h> |
| 25 | #include <linux/of_device.h> |
| 26 | #include <linux/thermal.h> |
| 27 | #include <linux/qpnp/qpnp-adc.h> |
| 28 | |
| 29 | #define QPNP_TM_DRIVER_NAME "qcom,qpnp-temp-alarm" |
| 30 | |
| 31 | enum qpnp_tm_registers { |
| 32 | QPNP_TM_REG_TYPE = 0x04, |
| 33 | QPNP_TM_REG_SUBTYPE = 0x05, |
| 34 | QPNP_TM_REG_STATUS = 0x08, |
| 35 | QPNP_TM_REG_SHUTDOWN_CTRL1 = 0x40, |
| 36 | QPNP_TM_REG_SHUTDOWN_CTRL2 = 0x42, |
| 37 | QPNP_TM_REG_ALARM_CTRL = 0x46, |
| 38 | }; |
| 39 | |
| 40 | #define QPNP_TM_TYPE 0x09 |
| 41 | #define QPNP_TM_SUBTYPE 0x08 |
| 42 | |
| 43 | #define STATUS_STAGE_MASK 0x03 |
| 44 | |
| 45 | #define SHUTDOWN_CTRL1_OVERRIDE_STAGE3 0x80 |
| 46 | #define SHUTDOWN_CTRL1_OVERRIDE_STAGE2 0x40 |
| 47 | #define SHUTDOWN_CTRL1_THRESHOLD_MASK 0x03 |
| 48 | |
| 49 | #define SHUTDOWN_CTRL2_CLEAR_STAGE3 0x80 |
| 50 | #define SHUTDOWN_CTRL2_CLEAR_STAGE2 0x40 |
| 51 | |
| 52 | #define ALARM_CTRL_FORCE_ENABLE 0x80 |
| 53 | #define ALARM_CTRL_FOLLOW_HW_ENABLE 0x01 |
| 54 | |
| 55 | #define TEMP_STAGE_STEP 20000 /* Stage step: 20.000 C */ |
| 56 | #define TEMP_STAGE_HYSTERESIS 2000 |
| 57 | |
| 58 | #define TEMP_THRESH_MIN 105000 /* Threshold Min: 105 C */ |
| 59 | #define TEMP_THRESH_STEP 5000 /* Threshold step: 5 C */ |
| 60 | |
| 61 | #define THRESH_MIN 0 |
| 62 | #define THRESH_MAX 3 |
| 63 | |
| 64 | /* Trip points from most critical to least critical */ |
| 65 | #define TRIP_STAGE3 0 |
| 66 | #define TRIP_STAGE2 1 |
| 67 | #define TRIP_STAGE1 2 |
| 68 | #define TRIP_NUM 3 |
| 69 | |
| 70 | enum qpnp_tm_adc_type { |
| 71 | QPNP_TM_ADC_NONE, /* Estimates temp based on overload level. */ |
| 72 | QPNP_TM_ADC_QPNP_ADC, |
| 73 | }; |
| 74 | |
| 75 | /* |
| 76 | * Temperature in millicelcius reported during stage 0 if no ADC is present and |
| 77 | * no value has been specified via device tree. |
| 78 | */ |
| 79 | #define DEFAULT_NO_ADC_TEMP 37000 |
| 80 | |
| 81 | struct qpnp_tm_chip { |
| 82 | struct delayed_work irq_work; |
| 83 | struct spmi_device *spmi_dev; |
| 84 | struct thermal_zone_device *tz_dev; |
| 85 | const char *tm_name; |
| 86 | enum qpnp_tm_adc_type adc_type; |
| 87 | unsigned long temperature; |
| 88 | enum thermal_device_mode mode; |
| 89 | unsigned int thresh; |
| 90 | unsigned int stage; |
| 91 | unsigned int prev_stage; |
| 92 | int irq; |
| 93 | enum qpnp_vadc_channels adc_channel; |
| 94 | u16 base_addr; |
| 95 | bool allow_software_override; |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 96 | struct qpnp_vadc_chip *vadc_dev; |
David Collins | 8836530 | 2012-07-17 09:34:57 -0700 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | /* Delay between TEMP_STAT IRQ going high and status value changing in ms. */ |
| 100 | #define STATUS_REGISTER_DELAY_MS 40 |
| 101 | |
| 102 | enum pmic_thermal_override_mode { |
| 103 | SOFTWARE_OVERRIDE_DISABLED = 0, |
| 104 | SOFTWARE_OVERRIDE_ENABLED, |
| 105 | }; |
| 106 | |
| 107 | static inline int qpnp_tm_read(struct qpnp_tm_chip *chip, u16 addr, u8 *buf, |
| 108 | int len) |
| 109 | { |
| 110 | int rc; |
| 111 | |
| 112 | rc = spmi_ext_register_readl(chip->spmi_dev->ctrl, |
| 113 | chip->spmi_dev->sid, chip->base_addr + addr, buf, len); |
| 114 | |
| 115 | if (rc) |
| 116 | dev_err(&chip->spmi_dev->dev, "%s: spmi_ext_register_readl() failed. sid=%d, addr=%04X, len=%d, rc=%d\n", |
| 117 | __func__, chip->spmi_dev->sid, chip->base_addr + addr, |
| 118 | len, rc); |
| 119 | |
| 120 | return rc; |
| 121 | } |
| 122 | |
| 123 | static inline int qpnp_tm_write(struct qpnp_tm_chip *chip, u16 addr, u8 *buf, |
| 124 | int len) |
| 125 | { |
| 126 | int rc; |
| 127 | |
| 128 | rc = spmi_ext_register_writel(chip->spmi_dev->ctrl, |
| 129 | chip->spmi_dev->sid, chip->base_addr + addr, buf, len); |
| 130 | |
| 131 | if (rc) |
| 132 | dev_err(&chip->spmi_dev->dev, "%s: spmi_ext_register_writel() failed. sid=%d, addr=%04X, len=%d, rc=%d\n", |
| 133 | __func__, chip->spmi_dev->sid, chip->base_addr + addr, |
| 134 | len, rc); |
| 135 | |
| 136 | return rc; |
| 137 | } |
| 138 | |
| 139 | |
| 140 | static inline int qpnp_tm_shutdown_override(struct qpnp_tm_chip *chip, |
| 141 | enum pmic_thermal_override_mode mode) |
| 142 | { |
| 143 | int rc = 0; |
| 144 | u8 reg; |
| 145 | |
| 146 | if (chip->allow_software_override) { |
| 147 | reg = chip->thresh & SHUTDOWN_CTRL1_THRESHOLD_MASK; |
| 148 | |
| 149 | if (mode == SOFTWARE_OVERRIDE_ENABLED) |
| 150 | reg |= SHUTDOWN_CTRL1_OVERRIDE_STAGE2 |
| 151 | | SHUTDOWN_CTRL1_OVERRIDE_STAGE3; |
| 152 | |
| 153 | rc = qpnp_tm_write(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, ®, 1); |
| 154 | } |
| 155 | |
| 156 | return rc; |
| 157 | } |
| 158 | |
| 159 | static int qpnp_tm_update_temp(struct qpnp_tm_chip *chip) |
| 160 | { |
| 161 | struct qpnp_vadc_result adc_result; |
| 162 | int rc; |
| 163 | |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 164 | rc = qpnp_vadc_read(chip->vadc_dev, chip->adc_channel, &adc_result); |
David Collins | 8836530 | 2012-07-17 09:34:57 -0700 | [diff] [blame] | 165 | if (!rc) |
| 166 | chip->temperature = adc_result.physical; |
| 167 | else |
| 168 | dev_err(&chip->spmi_dev->dev, "%s: qpnp_vadc_read(%d) failed, rc=%d\n", |
| 169 | __func__, chip->adc_channel, rc); |
| 170 | |
| 171 | return rc; |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | * This function initializes the internal temperature value based on only the |
| 176 | * current thermal stage and threshold. |
| 177 | */ |
| 178 | static int qpnp_tm_init_temp_no_adc(struct qpnp_tm_chip *chip) |
| 179 | { |
| 180 | int rc; |
| 181 | u8 reg; |
| 182 | |
| 183 | rc = qpnp_tm_read(chip, QPNP_TM_REG_STATUS, ®, 1); |
| 184 | if (rc < 0) |
| 185 | return rc; |
| 186 | |
| 187 | chip->stage = reg & STATUS_STAGE_MASK; |
| 188 | |
| 189 | if (chip->stage) |
| 190 | chip->temperature = chip->thresh * TEMP_THRESH_STEP + |
| 191 | (chip->stage - 1) * TEMP_STAGE_STEP + |
| 192 | TEMP_THRESH_MIN; |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * This function updates the internal temperature value based on the |
| 199 | * current thermal stage and threshold as well as the previous stage |
| 200 | */ |
| 201 | static int qpnp_tm_update_temp_no_adc(struct qpnp_tm_chip *chip) |
| 202 | { |
| 203 | unsigned int stage; |
| 204 | int rc; |
| 205 | u8 reg; |
| 206 | |
| 207 | rc = qpnp_tm_read(chip, QPNP_TM_REG_STATUS, ®, 1); |
| 208 | if (rc < 0) |
| 209 | return rc; |
| 210 | |
| 211 | stage = reg & STATUS_STAGE_MASK; |
| 212 | |
| 213 | if (stage > chip->stage) { |
| 214 | /* increasing stage, use lower bound */ |
| 215 | chip->temperature = (stage - 1) * TEMP_STAGE_STEP |
| 216 | + chip->thresh * TEMP_THRESH_STEP |
| 217 | + TEMP_STAGE_HYSTERESIS + TEMP_THRESH_MIN; |
| 218 | } else if (stage < chip->stage) { |
| 219 | /* decreasing stage, use upper bound */ |
| 220 | chip->temperature = stage * TEMP_STAGE_STEP |
| 221 | + chip->thresh * TEMP_THRESH_STEP |
| 222 | - TEMP_STAGE_HYSTERESIS + TEMP_THRESH_MIN; |
| 223 | } |
| 224 | |
| 225 | chip->stage = stage; |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | static int qpnp_tz_get_temp_no_adc(struct thermal_zone_device *thermal, |
| 231 | unsigned long *temperature) |
| 232 | { |
| 233 | struct qpnp_tm_chip *chip = thermal->devdata; |
| 234 | int rc; |
| 235 | |
| 236 | if (!temperature) |
| 237 | return -EINVAL; |
| 238 | |
| 239 | rc = qpnp_tm_update_temp_no_adc(chip); |
| 240 | if (rc < 0) |
| 241 | return rc; |
| 242 | |
| 243 | *temperature = chip->temperature; |
| 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | static int qpnp_tz_get_temp_qpnp_adc(struct thermal_zone_device *thermal, |
| 249 | unsigned long *temperature) |
| 250 | { |
| 251 | struct qpnp_tm_chip *chip = thermal->devdata; |
| 252 | int rc; |
| 253 | |
| 254 | if (!temperature) |
| 255 | return -EINVAL; |
| 256 | |
| 257 | rc = qpnp_tm_update_temp(chip); |
| 258 | if (rc < 0) { |
| 259 | dev_err(&chip->spmi_dev->dev, "%s: %s: adc read failed, rc = %d\n", |
| 260 | __func__, chip->tm_name, rc); |
| 261 | return rc; |
| 262 | } |
| 263 | |
| 264 | *temperature = chip->temperature; |
| 265 | |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | static int qpnp_tz_get_mode(struct thermal_zone_device *thermal, |
| 270 | enum thermal_device_mode *mode) |
| 271 | { |
| 272 | struct qpnp_tm_chip *chip = thermal->devdata; |
| 273 | |
| 274 | if (!mode) |
| 275 | return -EINVAL; |
| 276 | |
| 277 | *mode = chip->mode; |
| 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | static int qpnp_tz_set_mode(struct thermal_zone_device *thermal, |
| 283 | enum thermal_device_mode mode) |
| 284 | { |
| 285 | struct qpnp_tm_chip *chip = thermal->devdata; |
| 286 | int rc = 0; |
| 287 | |
| 288 | if (mode != chip->mode) { |
| 289 | if (mode == THERMAL_DEVICE_ENABLED) |
| 290 | rc = qpnp_tm_shutdown_override(chip, |
| 291 | SOFTWARE_OVERRIDE_ENABLED); |
| 292 | else |
| 293 | rc = qpnp_tm_shutdown_override(chip, |
| 294 | SOFTWARE_OVERRIDE_DISABLED); |
| 295 | |
| 296 | chip->mode = mode; |
| 297 | } |
| 298 | |
| 299 | return rc; |
| 300 | } |
| 301 | |
| 302 | static int qpnp_tz_get_trip_type(struct thermal_zone_device *thermal, |
| 303 | int trip, enum thermal_trip_type *type) |
| 304 | { |
| 305 | if (trip < 0 || !type) |
| 306 | return -EINVAL; |
| 307 | |
| 308 | switch (trip) { |
| 309 | case TRIP_STAGE3: |
| 310 | *type = THERMAL_TRIP_CRITICAL; |
| 311 | break; |
| 312 | case TRIP_STAGE2: |
| 313 | *type = THERMAL_TRIP_HOT; |
| 314 | break; |
| 315 | case TRIP_STAGE1: |
| 316 | *type = THERMAL_TRIP_HOT; |
| 317 | break; |
| 318 | default: |
| 319 | return -EINVAL; |
| 320 | } |
| 321 | |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | static int qpnp_tz_get_trip_temp(struct thermal_zone_device *thermal, |
| 326 | int trip, unsigned long *temperature) |
| 327 | { |
| 328 | struct qpnp_tm_chip *chip = thermal->devdata; |
| 329 | int thresh_temperature; |
| 330 | |
| 331 | if (trip < 0 || !temperature) |
| 332 | return -EINVAL; |
| 333 | |
| 334 | thresh_temperature = chip->thresh * TEMP_THRESH_STEP + TEMP_THRESH_MIN; |
| 335 | |
| 336 | switch (trip) { |
| 337 | case TRIP_STAGE3: |
| 338 | thresh_temperature += 2 * TEMP_STAGE_STEP; |
| 339 | break; |
| 340 | case TRIP_STAGE2: |
| 341 | thresh_temperature += TEMP_STAGE_STEP; |
| 342 | break; |
| 343 | case TRIP_STAGE1: |
| 344 | break; |
| 345 | default: |
| 346 | return -EINVAL; |
| 347 | } |
| 348 | |
| 349 | *temperature = thresh_temperature; |
| 350 | |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | static int qpnp_tz_get_crit_temp(struct thermal_zone_device *thermal, |
| 355 | unsigned long *temperature) |
| 356 | { |
| 357 | struct qpnp_tm_chip *chip = thermal->devdata; |
| 358 | |
| 359 | if (!temperature) |
| 360 | return -EINVAL; |
| 361 | |
| 362 | *temperature = chip->thresh * TEMP_THRESH_STEP + TEMP_THRESH_MIN + |
| 363 | 2 * TEMP_STAGE_STEP; |
| 364 | |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | static struct thermal_zone_device_ops qpnp_thermal_zone_ops_no_adc = { |
| 369 | .get_temp = qpnp_tz_get_temp_no_adc, |
| 370 | .get_mode = qpnp_tz_get_mode, |
| 371 | .set_mode = qpnp_tz_set_mode, |
| 372 | .get_trip_type = qpnp_tz_get_trip_type, |
| 373 | .get_trip_temp = qpnp_tz_get_trip_temp, |
| 374 | .get_crit_temp = qpnp_tz_get_crit_temp, |
| 375 | }; |
| 376 | |
| 377 | static struct thermal_zone_device_ops qpnp_thermal_zone_ops_qpnp_adc = { |
| 378 | .get_temp = qpnp_tz_get_temp_qpnp_adc, |
| 379 | .get_mode = qpnp_tz_get_mode, |
| 380 | .set_mode = qpnp_tz_set_mode, |
| 381 | .get_trip_type = qpnp_tz_get_trip_type, |
| 382 | .get_trip_temp = qpnp_tz_get_trip_temp, |
| 383 | .get_crit_temp = qpnp_tz_get_crit_temp, |
| 384 | }; |
| 385 | |
| 386 | static void qpnp_tm_work(struct work_struct *work) |
| 387 | { |
| 388 | struct delayed_work *dwork |
| 389 | = container_of(work, struct delayed_work, work); |
| 390 | struct qpnp_tm_chip *chip |
| 391 | = container_of(dwork, struct qpnp_tm_chip, irq_work); |
| 392 | int rc; |
| 393 | u8 reg; |
| 394 | |
| 395 | if (chip->adc_type == QPNP_TM_ADC_NONE) { |
| 396 | rc = qpnp_tm_update_temp_no_adc(chip); |
| 397 | if (rc < 0) |
| 398 | goto bail; |
| 399 | } else { |
| 400 | rc = qpnp_tm_read(chip, QPNP_TM_REG_STATUS, ®, 1); |
| 401 | if (rc < 0) |
| 402 | goto bail; |
| 403 | |
| 404 | chip->stage = reg & STATUS_STAGE_MASK; |
| 405 | |
| 406 | rc = qpnp_tm_update_temp(chip); |
| 407 | if (rc < 0) |
| 408 | goto bail; |
| 409 | } |
| 410 | |
| 411 | if (chip->stage != chip->prev_stage) { |
| 412 | chip->prev_stage = chip->stage; |
| 413 | |
| 414 | pr_crit("%s: PMIC Temp Alarm - stage=%u, threshold=%u, temperature=%lu mC\n", |
| 415 | chip->tm_name, chip->stage, chip->thresh, |
| 416 | chip->temperature); |
| 417 | |
| 418 | thermal_zone_device_update(chip->tz_dev); |
| 419 | |
| 420 | /* Notify user space */ |
| 421 | sysfs_notify(&chip->tz_dev->device.kobj, NULL, "type"); |
| 422 | } |
| 423 | |
| 424 | bail: |
| 425 | return; |
| 426 | } |
| 427 | |
| 428 | static irqreturn_t qpnp_tm_isr(int irq, void *data) |
| 429 | { |
| 430 | struct qpnp_tm_chip *chip = data; |
| 431 | |
| 432 | schedule_delayed_work(&chip->irq_work, |
| 433 | msecs_to_jiffies(STATUS_REGISTER_DELAY_MS) + 1); |
| 434 | |
| 435 | return IRQ_HANDLED; |
| 436 | } |
| 437 | |
| 438 | static int qpnp_tm_init_reg(struct qpnp_tm_chip *chip) |
| 439 | { |
| 440 | int rc = 0; |
| 441 | u8 reg; |
| 442 | |
| 443 | if (chip->thresh < THRESH_MIN || chip->thresh > THRESH_MAX) { |
| 444 | /* Read hardware threshold value if configuration is invalid. */ |
| 445 | rc = qpnp_tm_read(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, ®, 1); |
| 446 | if (rc < 0) |
| 447 | return rc; |
| 448 | chip->thresh = reg & SHUTDOWN_CTRL1_THRESHOLD_MASK; |
| 449 | } |
| 450 | |
| 451 | /* |
| 452 | * Set threshold and disable software override of stage 2 and 3 |
| 453 | * shutdowns. |
| 454 | */ |
| 455 | reg = chip->thresh & SHUTDOWN_CTRL1_THRESHOLD_MASK; |
| 456 | rc = qpnp_tm_write(chip, QPNP_TM_REG_SHUTDOWN_CTRL1, ®, 1); |
| 457 | if (rc < 0) |
| 458 | return rc; |
| 459 | |
| 460 | /* Enable the thermal alarm PMIC module in always-on mode. */ |
| 461 | reg = ALARM_CTRL_FORCE_ENABLE; |
| 462 | rc = qpnp_tm_write(chip, QPNP_TM_REG_ALARM_CTRL, ®, 1); |
| 463 | |
| 464 | return rc; |
| 465 | } |
| 466 | |
| 467 | static int __devinit qpnp_tm_probe(struct spmi_device *spmi) |
| 468 | { |
| 469 | struct device_node *node; |
| 470 | struct resource *res; |
| 471 | struct qpnp_tm_chip *chip; |
| 472 | struct thermal_zone_device_ops *tz_ops; |
| 473 | char *tm_name; |
| 474 | u32 default_temperature; |
| 475 | int rc = 0; |
| 476 | u8 raw_type[2], type, subtype; |
| 477 | |
| 478 | if (!spmi || !(&spmi->dev) || !spmi->dev.of_node) { |
| 479 | dev_err(&spmi->dev, "%s: device tree node not found\n", |
| 480 | __func__); |
| 481 | return -EINVAL; |
| 482 | } |
| 483 | |
| 484 | node = spmi->dev.of_node; |
| 485 | |
| 486 | chip = kzalloc(sizeof(struct qpnp_tm_chip), GFP_KERNEL); |
| 487 | if (!chip) { |
| 488 | dev_err(&spmi->dev, "%s: Can't allocate qpnp_tm_chip\n", |
| 489 | __func__); |
| 490 | return -ENOMEM; |
| 491 | } |
| 492 | |
| 493 | dev_set_drvdata(&spmi->dev, chip); |
| 494 | |
| 495 | res = spmi_get_resource(spmi, NULL, IORESOURCE_MEM, 0); |
| 496 | if (!res) { |
| 497 | dev_err(&spmi->dev, "%s: node is missing base address\n", |
| 498 | __func__); |
| 499 | rc = -EINVAL; |
| 500 | goto free_chip; |
| 501 | } |
| 502 | chip->base_addr = res->start; |
| 503 | chip->spmi_dev = spmi; |
| 504 | |
| 505 | chip->irq = spmi_get_irq(spmi, NULL, 0); |
| 506 | if (chip->irq < 0) { |
| 507 | rc = chip->irq; |
| 508 | dev_err(&spmi->dev, "%s: node is missing irq, rc=%d\n", |
| 509 | __func__, rc); |
| 510 | goto free_chip; |
| 511 | } |
| 512 | |
| 513 | chip->tm_name = of_get_property(node, "label", NULL); |
| 514 | if (chip->tm_name == NULL) { |
| 515 | dev_err(&spmi->dev, "%s: node is missing label\n", |
| 516 | __func__); |
| 517 | rc = -EINVAL; |
| 518 | goto free_chip; |
| 519 | } |
| 520 | |
| 521 | tm_name = kstrdup(chip->tm_name, GFP_KERNEL); |
| 522 | if (tm_name == NULL) { |
| 523 | dev_err(&spmi->dev, "%s: could not allocate memory for label\n", |
| 524 | __func__); |
| 525 | rc = -ENOMEM; |
| 526 | goto free_chip; |
| 527 | } |
| 528 | chip->tm_name = tm_name; |
| 529 | |
| 530 | INIT_DELAYED_WORK(&chip->irq_work, qpnp_tm_work); |
| 531 | |
| 532 | /* These bindings are optional, so it is okay if they are not found. */ |
| 533 | chip->thresh = THRESH_MAX + 1; |
| 534 | rc = of_property_read_u32(node, "qcom,threshold-set", &chip->thresh); |
| 535 | if (!rc && (chip->thresh < THRESH_MIN || chip->thresh > THRESH_MAX)) |
| 536 | dev_err(&spmi->dev, "%s: invalid qcom,threshold-set=%u specified\n", |
| 537 | __func__, chip->thresh); |
| 538 | |
| 539 | chip->adc_type = QPNP_TM_ADC_NONE; |
| 540 | rc = of_property_read_u32(node, "qcom,channel-num", &chip->adc_channel); |
| 541 | if (!rc) { |
| 542 | if (chip->adc_channel < 0 || chip->adc_channel >= ADC_MAX_NUM) { |
| 543 | dev_err(&spmi->dev, "%s: invalid qcom,channel-num=%d specified\n", |
| 544 | __func__, chip->adc_channel); |
| 545 | } else { |
| 546 | chip->adc_type = QPNP_TM_ADC_QPNP_ADC; |
Siddartha Mohanadoss | 3cb2b6b | 2013-06-21 12:07:05 -0700 | [diff] [blame] | 547 | chip->vadc_dev = qpnp_get_vadc(&spmi->dev, |
| 548 | "temp_alarm"); |
| 549 | if (IS_ERR(chip->vadc_dev)) { |
| 550 | rc = PTR_ERR(chip->vadc_dev); |
| 551 | if (rc != -EPROBE_DEFER) |
| 552 | pr_err("vadc property missing\n"); |
David Collins | 8836530 | 2012-07-17 09:34:57 -0700 | [diff] [blame] | 553 | goto err_cancel_work; |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | if (chip->adc_type == QPNP_TM_ADC_QPNP_ADC) |
| 559 | tz_ops = &qpnp_thermal_zone_ops_qpnp_adc; |
| 560 | else |
| 561 | tz_ops = &qpnp_thermal_zone_ops_no_adc; |
| 562 | |
| 563 | chip->allow_software_override |
| 564 | = of_property_read_bool(node, "qcom,allow-override"); |
| 565 | |
| 566 | default_temperature = DEFAULT_NO_ADC_TEMP; |
| 567 | rc = of_property_read_u32(node, "qcom,default-temp", |
| 568 | &default_temperature); |
| 569 | chip->temperature = default_temperature; |
| 570 | |
| 571 | rc = qpnp_tm_read(chip, QPNP_TM_REG_TYPE, raw_type, 2); |
| 572 | if (rc) { |
| 573 | dev_err(&spmi->dev, "%s: could not read type register, rc=%d\n", |
| 574 | __func__, rc); |
| 575 | goto err_cancel_work; |
| 576 | } |
| 577 | type = raw_type[0]; |
| 578 | subtype = raw_type[1]; |
| 579 | |
| 580 | if (type != QPNP_TM_TYPE || subtype != QPNP_TM_SUBTYPE) { |
| 581 | dev_err(&spmi->dev, "%s: invalid type=%02X or subtype=%02X register value\n", |
| 582 | __func__, type, subtype); |
| 583 | rc = -ENODEV; |
| 584 | goto err_cancel_work; |
| 585 | } |
| 586 | |
| 587 | rc = qpnp_tm_init_reg(chip); |
| 588 | if (rc) { |
| 589 | dev_err(&spmi->dev, "%s: qpnp_tm_init_reg() failed, rc=%d\n", |
| 590 | __func__, rc); |
| 591 | goto err_cancel_work; |
| 592 | } |
| 593 | |
| 594 | if (chip->adc_type == QPNP_TM_ADC_NONE) { |
| 595 | rc = qpnp_tm_init_temp_no_adc(chip); |
| 596 | if (rc) { |
| 597 | dev_err(&spmi->dev, "%s: qpnp_tm_init_temp_no_adc() failed, rc=%d\n", |
| 598 | __func__, rc); |
| 599 | goto err_cancel_work; |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | /* Start in HW control; switch to SW control when user changes mode. */ |
| 604 | chip->mode = THERMAL_DEVICE_DISABLED; |
| 605 | rc = qpnp_tm_shutdown_override(chip, SOFTWARE_OVERRIDE_DISABLED); |
| 606 | if (rc) { |
| 607 | dev_err(&spmi->dev, "%s: qpnp_tm_shutdown_override() failed, rc=%d\n", |
| 608 | __func__, rc); |
| 609 | goto err_cancel_work; |
| 610 | } |
| 611 | |
| 612 | chip->tz_dev = thermal_zone_device_register(tm_name, TRIP_NUM, chip, |
| 613 | tz_ops, 0, 0, 0, 0); |
| 614 | if (chip->tz_dev == NULL) { |
| 615 | dev_err(&spmi->dev, "%s: thermal_zone_device_register() failed.\n", |
| 616 | __func__); |
| 617 | rc = -ENODEV; |
| 618 | goto err_cancel_work; |
| 619 | } |
| 620 | |
| 621 | rc = request_irq(chip->irq, qpnp_tm_isr, IRQF_TRIGGER_RISING, tm_name, |
| 622 | chip); |
| 623 | if (rc < 0) { |
| 624 | dev_err(&spmi->dev, "%s: request_irq(%d) failed: %d\n", |
| 625 | __func__, chip->irq, rc); |
| 626 | goto err_free_tz; |
| 627 | } |
| 628 | |
| 629 | return 0; |
| 630 | |
| 631 | err_free_tz: |
| 632 | thermal_zone_device_unregister(chip->tz_dev); |
| 633 | err_cancel_work: |
| 634 | cancel_delayed_work_sync(&chip->irq_work); |
| 635 | kfree(chip->tm_name); |
| 636 | free_chip: |
| 637 | dev_set_drvdata(&spmi->dev, NULL); |
| 638 | kfree(chip); |
| 639 | return rc; |
| 640 | } |
| 641 | |
| 642 | static int __devexit qpnp_tm_remove(struct spmi_device *spmi) |
| 643 | { |
| 644 | struct qpnp_tm_chip *chip = dev_get_drvdata(&spmi->dev); |
| 645 | |
| 646 | dev_set_drvdata(&spmi->dev, NULL); |
| 647 | thermal_zone_device_unregister(chip->tz_dev); |
| 648 | kfree(chip->tm_name); |
| 649 | qpnp_tm_shutdown_override(chip, SOFTWARE_OVERRIDE_DISABLED); |
| 650 | free_irq(chip->irq, chip); |
| 651 | cancel_delayed_work_sync(&chip->irq_work); |
| 652 | kfree(chip); |
| 653 | |
| 654 | return 0; |
| 655 | } |
| 656 | |
| 657 | #ifdef CONFIG_PM |
| 658 | static int qpnp_tm_suspend(struct device *dev) |
| 659 | { |
| 660 | struct qpnp_tm_chip *chip = dev_get_drvdata(dev); |
| 661 | |
| 662 | /* Clear override bits in suspend to allow hardware control */ |
| 663 | qpnp_tm_shutdown_override(chip, SOFTWARE_OVERRIDE_DISABLED); |
| 664 | |
| 665 | return 0; |
| 666 | } |
| 667 | |
| 668 | static int qpnp_tm_resume(struct device *dev) |
| 669 | { |
| 670 | struct qpnp_tm_chip *chip = dev_get_drvdata(dev); |
| 671 | |
| 672 | /* Override hardware actions so software can control */ |
| 673 | if (chip->mode == THERMAL_DEVICE_ENABLED) |
| 674 | qpnp_tm_shutdown_override(chip, SOFTWARE_OVERRIDE_ENABLED); |
| 675 | |
| 676 | return 0; |
| 677 | } |
| 678 | |
| 679 | static const struct dev_pm_ops qpnp_tm_pm_ops = { |
| 680 | .suspend = qpnp_tm_suspend, |
| 681 | .resume = qpnp_tm_resume, |
| 682 | }; |
| 683 | |
| 684 | #define QPNP_TM_PM_OPS (&qpnp_tm_pm_ops) |
| 685 | #else |
| 686 | #define QPNP_TM_PM_OPS NULL |
| 687 | #endif |
| 688 | |
| 689 | static struct of_device_id qpnp_tm_match_table[] = { |
| 690 | { .compatible = QPNP_TM_DRIVER_NAME, }, |
| 691 | {} |
| 692 | }; |
| 693 | |
| 694 | static const struct spmi_device_id qpnp_tm_id[] = { |
| 695 | { QPNP_TM_DRIVER_NAME, 0 }, |
| 696 | {} |
| 697 | }; |
| 698 | |
| 699 | static struct spmi_driver qpnp_tm_driver = { |
| 700 | .driver = { |
| 701 | .name = QPNP_TM_DRIVER_NAME, |
| 702 | .of_match_table = qpnp_tm_match_table, |
| 703 | .owner = THIS_MODULE, |
| 704 | .pm = QPNP_TM_PM_OPS, |
| 705 | }, |
| 706 | .probe = qpnp_tm_probe, |
| 707 | .remove = __devexit_p(qpnp_tm_remove), |
| 708 | .id_table = qpnp_tm_id, |
| 709 | }; |
| 710 | |
| 711 | int __init qpnp_tm_init(void) |
| 712 | { |
| 713 | return spmi_driver_register(&qpnp_tm_driver); |
| 714 | } |
| 715 | |
| 716 | static void __exit qpnp_tm_exit(void) |
| 717 | { |
| 718 | spmi_driver_unregister(&qpnp_tm_driver); |
| 719 | } |
| 720 | |
| 721 | module_init(qpnp_tm_init); |
| 722 | module_exit(qpnp_tm_exit); |
| 723 | |
| 724 | MODULE_DESCRIPTION("QPNP PMIC Temperature Alarm driver"); |
| 725 | MODULE_LICENSE("GPL v2"); |