static int pm8xxx_tm_suspend(struct device *dev) { struct platform_device *pdev = to_platform_device(dev); struct pm8xxx_tm_chip *chip = platform_get_drvdata(pdev); /* Clear override bits in suspend to allow hardware control */ pm8xxx_tm_shutdown_override(chip, SOFTWARE_OVERRIDE_DISABLED); return 0; }
static int pm8xxx_tm_resume(struct device *dev) { struct platform_device *pdev = to_platform_device(dev); struct pm8xxx_tm_chip *chip = platform_get_drvdata(pdev); /* Override hardware actions so software can control */ if (chip->mode == THERMAL_DEVICE_ENABLED) pm8xxx_tm_shutdown_override(chip, SOFTWARE_OVERRIDE_ENABLED); return 0; }
static int __devexit pm8xxx_tm_remove(struct platform_device *pdev) { struct pm8xxx_tm_chip *chip = platform_get_drvdata(pdev); if (chip) { platform_set_drvdata(pdev, NULL); cancel_work_sync(&chip->irq_work); free_irq(chip->overtemp_irq, chip); free_irq(chip->tempstat_irq, chip); pm8xxx_tm_shutdown_override(chip, SOFTWARE_OVERRIDE_DISABLED); thermal_zone_device_unregister(chip->tz_dev); kfree(chip); } return 0; }
static int __devinit pm8xxx_tm_probe(struct platform_device *pdev) { const struct pm8xxx_tm_core_data *cdata = pdev->dev.platform_data; struct thermal_zone_device_ops *tz_ops; struct pm8xxx_tm_chip *chip; struct resource *res; int rc = 0; if (!cdata) { pr_err("missing core data\n"); return -EINVAL; } chip = kzalloc(sizeof(struct pm8xxx_tm_chip), GFP_KERNEL); if (chip == NULL) { pr_err("kzalloc() failed.\n"); return -ENOMEM; } chip->dev = &pdev->dev; memcpy(&(chip->cdata), cdata, sizeof(struct pm8xxx_tm_core_data)); res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, chip->cdata.irq_name_temp_stat); if (res) { chip->tempstat_irq = res->start; } else { pr_err("temp stat IRQ not specified\n"); goto err_free_chip; } res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, chip->cdata.irq_name_over_temp); if (res) { chip->overtemp_irq = res->start; } else { pr_err("over temp IRQ not specified\n"); goto err_free_chip; } rc = pm8xxx_init_adc(chip, true); if (rc < 0) { pr_err("Unable to initialize adc\n"); goto err_free_chip; } /* Select proper thermal zone ops functions based on ADC type. */ if (chip->cdata.adc_type == PM8XXX_TM_ADC_PM8XXX_ADC) tz_ops = &pm8xxx_thermal_zone_ops_pm8xxx_adc; else if (chip->cdata.adc_type == PM8XXX_TM_ADC_PM8058_ADC) tz_ops = &pm8xxx_thermal_zone_ops_pm8058_adc; else tz_ops = &pm8xxx_thermal_zone_ops_no_adc; chip->tz_dev = thermal_zone_device_register(chip->cdata.tm_name, TRIP_NUM, chip, tz_ops, 0, 0, 0, 0); if (chip->tz_dev == NULL) { pr_err("thermal_zone_device_register() failed.\n"); rc = -ENODEV; goto err_fail_adc; } rc = pm8xxx_tm_init_reg(chip); if (rc < 0) goto err_free_tz; rc = pm8xxx_tm_shutdown_override(chip, SOFTWARE_OVERRIDE_DISABLED); if (rc < 0) goto err_free_tz; if (chip->cdata.adc_type == PM8XXX_TM_ADC_NONE) { rc = pm8xxx_tm_init_temp_no_adc(chip); if (rc < 0) goto err_free_tz; } /* Start in HW control; switch to SW control when user changes mode. */ chip->mode = THERMAL_DEVICE_DISABLED; thermal_zone_device_update(chip->tz_dev); INIT_DELAYED_WORK(&chip->irq_work, pm8xxx_tm_work); rc = request_irq(chip->tempstat_irq, pm8xxx_tm_isr, IRQF_TRIGGER_RISING, chip->cdata.irq_name_temp_stat, chip); if (rc < 0) { pr_err("request_irq(%d) failed: %d\n", chip->tempstat_irq, rc); goto err_cancel_work; } rc = request_irq(chip->overtemp_irq, pm8xxx_tm_isr, IRQF_TRIGGER_RISING, chip->cdata.irq_name_over_temp, chip); if (rc < 0) { pr_err("request_irq(%d) failed: %d\n", chip->overtemp_irq, rc); goto err_free_irq_tempstat; } platform_set_drvdata(pdev, chip); pr_info("OK\n"); return 0; err_free_irq_tempstat: free_irq(chip->tempstat_irq, chip); err_cancel_work: cancel_delayed_work_sync(&chip->irq_work); err_free_tz: thermal_zone_device_unregister(chip->tz_dev); err_fail_adc: pm8xxx_init_adc(chip, false); err_free_chip: kfree(chip); return rc; }
static int pm8xxx_tz_get_temp_pm8058_adc(struct thermal_zone_device *thermal, unsigned long *temp) { struct pm8xxx_tm_chip *chip = thermal->devdata; DECLARE_COMPLETION_ONSTACK(wait); struct adc_chan_result adc_result = { .physical = 0lu, }; int rc; if (!chip || !temp) return -EINVAL; *temp = chip->temp; rc = adc_channel_request_conv(chip->adc_handle, &wait); if (rc < 0) { pr_err("%s: adc_channel_request_conv() failed, rc = %d\n", __func__, rc); return rc; } wait_for_completion(&wait); rc = adc_channel_read_result(chip->adc_handle, &adc_result); if (rc < 0) { pr_err("%s: adc_channel_read_result() failed, rc = %d\n", __func__, rc); return rc; } *temp = adc_result.physical; chip->temp = adc_result.physical; return 0; } static int pm8xxx_tz_get_temp_pm8xxx_adc(struct thermal_zone_device *thermal, unsigned long *temp) { struct pm8xxx_tm_chip *chip = thermal->devdata; struct pm8xxx_adc_chan_result result = { .physical = 0lu, }; int rc; if (!chip || !temp) return -EINVAL; *temp = chip->temp; rc = pm8xxx_adc_read(chip->cdata.adc_channel, &result); if (rc < 0) { pr_err("%s: adc_channel_read_result() failed, rc = %d\n", chip->cdata.tm_name, rc); return rc; } *temp = result.physical; chip->temp = result.physical; return 0; } static int pm8xxx_tz_get_mode(struct thermal_zone_device *thermal, enum thermal_device_mode *mode) { struct pm8xxx_tm_chip *chip = thermal->devdata; if (!chip || !mode) return -EINVAL; *mode = chip->mode; return 0; } static int pm8xxx_tz_set_mode(struct thermal_zone_device *thermal, enum thermal_device_mode mode) { struct pm8xxx_tm_chip *chip = thermal->devdata; if (!chip) return -EINVAL; /* Mask software override requests if they are not allowed. */ if (!chip->cdata.allow_software_override) mode = THERMAL_DEVICE_DISABLED; if (mode != chip->mode) { if (mode == THERMAL_DEVICE_ENABLED) pm8xxx_tm_shutdown_override(chip, SOFTWARE_OVERRIDE_ENABLED); else pm8xxx_tm_shutdown_override(chip, SOFTWARE_OVERRIDE_DISABLED); } chip->mode = mode; return 0; } static int pm8xxx_tz_get_trip_type(struct thermal_zone_device *thermal, int trip, enum thermal_trip_type *type) { if (trip < 0 || !type) return -EINVAL; switch (trip) { case TRIP_STAGE3: *type = THERMAL_TRIP_CRITICAL; break; case TRIP_STAGE2: *type = THERMAL_TRIP_HOT; break; case TRIP_STAGE1: *type = THERMAL_TRIP_HOT; break; default: return -EINVAL; } return 0; } static int pm8xxx_tz_get_trip_temp(struct thermal_zone_device *thermal, int trip, unsigned long *temp) { struct pm8xxx_tm_chip *chip = thermal->devdata; int thresh_temp; if (!chip || trip < 0 || !temp) return -EINVAL; thresh_temp = chip->thresh * TEMP_THRESH_STEP + TEMP_THRESH_MIN; switch (trip) { case TRIP_STAGE3: thresh_temp += 2 * TEMP_STAGE_STEP; break; case TRIP_STAGE2: thresh_temp += TEMP_STAGE_STEP; break; case TRIP_STAGE1: break; default: return -EINVAL; } *temp = thresh_temp; return 0; } static int pm8xxx_tz_get_crit_temp(struct thermal_zone_device *thermal, unsigned long *temp) { struct pm8xxx_tm_chip *chip = thermal->devdata; if (!chip || !temp) return -EINVAL; *temp = chip->thresh * TEMP_THRESH_STEP + TEMP_THRESH_MIN + 2 * TEMP_STAGE_STEP; return 0; } static struct thermal_zone_device_ops pm8xxx_thermal_zone_ops_no_adc = { .get_temp = pm8xxx_tz_get_temp_no_adc, .get_mode = pm8xxx_tz_get_mode, .set_mode = pm8xxx_tz_set_mode, .get_trip_type = pm8xxx_tz_get_trip_type, .get_trip_temp = pm8xxx_tz_get_trip_temp, .get_crit_temp = pm8xxx_tz_get_crit_temp, }; static struct thermal_zone_device_ops pm8xxx_thermal_zone_ops_pm8xxx_adc = { .get_temp = pm8xxx_tz_get_temp_pm8xxx_adc, .get_mode = pm8xxx_tz_get_mode, .set_mode = pm8xxx_tz_set_mode, .get_trip_type = pm8xxx_tz_get_trip_type, .get_trip_temp = pm8xxx_tz_get_trip_temp, .get_crit_temp = pm8xxx_tz_get_crit_temp, }; static struct thermal_zone_device_ops pm8xxx_thermal_zone_ops_pm8058_adc = { .get_temp = pm8xxx_tz_get_temp_pm8058_adc, .get_mode = pm8xxx_tz_get_mode, .set_mode = pm8xxx_tz_set_mode, .get_trip_type = pm8xxx_tz_get_trip_type, .get_trip_temp = pm8xxx_tz_get_trip_temp, .get_crit_temp = pm8xxx_tz_get_crit_temp, }; static void pm8xxx_tm_work(struct work_struct *work) { struct delayed_work *dwork = container_of(work, struct delayed_work, work); struct pm8xxx_tm_chip *chip = container_of(dwork, struct pm8xxx_tm_chip, irq_work); unsigned long temp = 0; int rc, stage, thresh; u8 reg; rc = pm8xxx_tm_read_ctrl(chip, ®); if (rc < 0) goto bail; /* Clear status bits. */ if (reg & (TEMP_ALARM_CTRL_ST2_SD | TEMP_ALARM_CTRL_ST3_SD)) { reg &= ~(TEMP_ALARM_CTRL_ST2_SD | TEMP_ALARM_CTRL_ST3_SD | TEMP_ALARM_CTRL_STATUS_MASK); pm8xxx_tm_write_ctrl(chip, reg); } stage = (reg & TEMP_ALARM_CTRL_STATUS_MASK) >> TEMP_ALARM_CTRL_STATUS_SHIFT; thresh = (reg & TEMP_ALARM_CTRL_THRESH_MASK) >> TEMP_ALARM_CTRL_THRESH_SHIFT; thermal_zone_device_update(chip->tz_dev); if (stage != chip->prev_stage) { chip->prev_stage = stage; switch (chip->cdata.adc_type) { case PM8XXX_TM_ADC_NONE: rc = pm8xxx_tz_get_temp_no_adc(chip->tz_dev, &temp); break; case PM8XXX_TM_ADC_PM8058_ADC: rc = pm8xxx_tz_get_temp_pm8058_adc(chip->tz_dev, &temp); break; case PM8XXX_TM_ADC_PM8XXX_ADC: rc = pm8xxx_tz_get_temp_pm8xxx_adc(chip->tz_dev, &temp); break; } if (rc < 0) goto bail; pr_crit("%s: PMIC Temp Alarm - stage=%u, threshold=%u, temp=%lu mC\n", chip->cdata.tm_name, stage, thresh, temp); /* Notify user space */ sysfs_notify(&chip->tz_dev->device.kobj, NULL, "type"); } bail: return; } static irqreturn_t pm8xxx_tm_isr(int irq, void *data) { struct pm8xxx_tm_chip *chip = data; schedule_delayed_work(&chip->irq_work, msecs_to_jiffies(STATUS_REGISTER_DELAY_MS) + 1); return IRQ_HANDLED; } static int pm8xxx_tm_init_reg(struct pm8xxx_tm_chip *chip) { int rc; u8 reg; rc = pm8xxx_tm_read_ctrl(chip, ®); if (rc < 0) return rc; chip->stage = (reg & TEMP_ALARM_CTRL_STATUS_MASK) >> TEMP_ALARM_CTRL_STATUS_SHIFT; chip->temp = 0; /* Use temperature threshold set 0: (105, 125, 145) */ chip->thresh = 0; reg = (chip->thresh << TEMP_ALARM_CTRL_THRESH_SHIFT) & TEMP_ALARM_CTRL_THRESH_MASK; rc = pm8xxx_tm_write_ctrl(chip, reg); if (rc < 0) return rc; /* * Set the PMIC temperature alarm module to be always on. This ensures * that die temperature monitoring is active even if CXO is disabled * (i.e. when sleep_b is low). This is necessary since CXO can be * disabled while the system is still heavily loaded. Also, using * the alway-on instead of PWM-enabled configurations ensures that the * die temperature can be measured by the PMIC ADC without reconfiguring * the temperature alarm module first. */ rc = pm8xxx_tm_write_pwm(chip, TEMP_ALARM_PWM_EN_ALWAYS); return rc; } static int pm8xxx_init_adc(struct pm8xxx_tm_chip *chip, bool enable) { int rc = 0; if (chip->cdata.adc_type == PM8XXX_TM_ADC_PM8058_ADC) { if (enable) { rc = adc_channel_open(chip->cdata.adc_channel, &(chip->adc_handle)); if (rc < 0) pr_err("adc_channel_open() failed.\n"); } else { adc_channel_close(chip->adc_handle); } } return rc; }
static int pm8xxx_tz_get_temp_pm8xxx_adc(struct thermal_zone_device *thermal, unsigned long *temp) { struct pm8xxx_tm_chip *chip = thermal->devdata; struct pm8xxx_adc_chan_result result = { .physical = 0lu, }; int rc; if (!chip || !temp) return -EINVAL; *temp = chip->temp; rc = pm8xxx_adc_read(chip->cdata.adc_channel, &result); if (rc < 0) { pr_err("%s: adc_channel_read_result() failed, rc = %d\n", chip->cdata.tm_name, rc); return rc; } *temp = result.physical; chip->temp = result.physical; return 0; } static int pm8xxx_tz_get_mode(struct thermal_zone_device *thermal, enum thermal_device_mode *mode) { struct pm8xxx_tm_chip *chip = thermal->devdata; if (!chip || !mode) return -EINVAL; *mode = chip->mode; return 0; } static int pm8xxx_tz_set_mode(struct thermal_zone_device *thermal, enum thermal_device_mode mode) { struct pm8xxx_tm_chip *chip = thermal->devdata; if (!chip) return -EINVAL; if (mode != chip->mode) { if (mode == THERMAL_DEVICE_ENABLED) pm8xxx_tm_shutdown_override(chip, SOFTWARE_OVERRIDE_ENABLED); else pm8xxx_tm_shutdown_override(chip, SOFTWARE_OVERRIDE_DISABLED); } chip->mode = mode; return 0; } static int pm8xxx_tz_get_trip_type(struct thermal_zone_device *thermal, int trip, enum thermal_trip_type *type) { if (trip < 0 || !type) return -EINVAL; switch (trip) { case TRIP_STAGE3: *type = THERMAL_TRIP_CRITICAL; break; case TRIP_STAGE2: *type = THERMAL_TRIP_HOT; break; case TRIP_STAGE1: *type = THERMAL_TRIP_HOT; break; default: return -EINVAL; } return 0; } static int pm8xxx_tz_get_trip_temp(struct thermal_zone_device *thermal, int trip, unsigned long *temp) { struct pm8xxx_tm_chip *chip = thermal->devdata; int thresh_temp; if (!chip || trip < 0 || !temp) return -EINVAL; thresh_temp = chip->thresh * TEMP_THRESH_STEP + TEMP_THRESH_MIN; switch (trip) { case TRIP_STAGE3: thresh_temp += 2 * TEMP_STAGE_STEP; break; case TRIP_STAGE2: thresh_temp += TEMP_STAGE_STEP; break; case TRIP_STAGE1: break; default: return -EINVAL; } *temp = thresh_temp; return 0; } static int pm8xxx_tz_get_crit_temp(struct thermal_zone_device *thermal, unsigned long *temp) { struct pm8xxx_tm_chip *chip = thermal->devdata; if (!chip || !temp) return -EINVAL; *temp = chip->thresh * TEMP_THRESH_STEP + TEMP_THRESH_MIN + 2 * TEMP_STAGE_STEP; return 0; } static struct thermal_zone_device_ops pm8xxx_thermal_zone_ops_no_adc = { .get_temp = pm8xxx_tz_get_temp_no_adc, .get_mode = pm8xxx_tz_get_mode, .set_mode = pm8xxx_tz_set_mode, .get_trip_type = pm8xxx_tz_get_trip_type, .get_trip_temp = pm8xxx_tz_get_trip_temp, .get_crit_temp = pm8xxx_tz_get_crit_temp, }; static struct thermal_zone_device_ops pm8xxx_thermal_zone_ops_pm8xxx_adc = { .get_temp = pm8xxx_tz_get_temp_pm8xxx_adc, .get_mode = pm8xxx_tz_get_mode, .set_mode = pm8xxx_tz_set_mode, .get_trip_type = pm8xxx_tz_get_trip_type, .get_trip_temp = pm8xxx_tz_get_trip_temp, .get_crit_temp = pm8xxx_tz_get_crit_temp, }; static void pm8xxx_tm_work(struct work_struct *work) { struct pm8xxx_tm_chip *chip = container_of(work, struct pm8xxx_tm_chip, irq_work); int rc; u8 reg; rc = pm8xxx_tm_read_ctrl(chip, ®); if (rc < 0) goto bail; if (chip->cdata.adc_type == PM8XXX_TM_ADC_NONE) { rc = pm8xxx_tm_update_temp_no_adc(chip); if (rc < 0) goto bail; pr_info("%s: Temp Alarm - stage=%u, threshold=%u, " "temp=%lu mC\n", chip->cdata.tm_name, chip->stage, chip->thresh, chip->temp); } else { chip->stage = (reg & TEMP_ALARM_CTRL_STATUS_MASK) >> TEMP_ALARM_CTRL_STATUS_SHIFT; chip->thresh = (reg & TEMP_ALARM_CTRL_THRESH_MASK) >> TEMP_ALARM_CTRL_THRESH_SHIFT; pr_info("%s: Temp Alarm - stage=%u, threshold=%u\n", chip->cdata.tm_name, chip->stage, chip->thresh); } /* Clear status bits. */ if (reg & (TEMP_ALARM_CTRL_ST2_SD | TEMP_ALARM_CTRL_ST3_SD)) { reg &= ~(TEMP_ALARM_CTRL_ST2_SD | TEMP_ALARM_CTRL_ST3_SD | TEMP_ALARM_CTRL_STATUS_MASK); pm8xxx_tm_write_ctrl(chip, reg); } thermal_zone_device_update(chip->tz_dev); /* Notify user space */ if (chip->mode == THERMAL_DEVICE_ENABLED) kobject_uevent(&chip->tz_dev->device.kobj, KOBJ_CHANGE); bail: enable_irq(chip->tempstat_irq); enable_irq(chip->overtemp_irq); } static irqreturn_t pm8xxx_tm_isr(int irq, void *data) { struct pm8xxx_tm_chip *chip = data; disable_irq_nosync(chip->tempstat_irq); disable_irq_nosync(chip->overtemp_irq); schedule_work(&chip->irq_work); return IRQ_HANDLED; } static int pm8xxx_tm_init_reg(struct pm8xxx_tm_chip *chip) { int rc; u8 reg; rc = pm8xxx_tm_read_ctrl(chip, ®); if (rc < 0) return rc; chip->stage = (reg & TEMP_ALARM_CTRL_STATUS_MASK) >> TEMP_ALARM_CTRL_STATUS_SHIFT; chip->temp = 0; /* Use temperature threshold set 0: (105, 125, 145) */ chip->thresh = 0; reg = (chip->thresh << TEMP_ALARM_CTRL_THRESH_SHIFT) & TEMP_ALARM_CTRL_THRESH_MASK; rc = pm8xxx_tm_write_ctrl(chip, reg); if (rc < 0) return rc; /* * Set the PMIC alarm module PWM to have a frequency of 8 Hz. This * helps cut down on the number of unnecessary interrupts fired when * changing between thermal stages. Also, Enable the over temperature * PWM whenever the PMIC is enabled. */ reg = (1 << TEMP_ALARM_PWM_EN_SHIFT) | (3 << TEMP_ALARM_PWM_PER_PRE_SHIFT) | (3 << TEMP_ALARM_PWM_PER_DIV_SHIFT); rc = pm8xxx_tm_write_pwm(chip, reg); return rc; }
static int pm8xxx_tz_get_temp_pm8058_adc(struct thermal_zone_device *thermal, unsigned long *temp) { struct pm8xxx_tm_chip *chip = thermal->devdata; DECLARE_COMPLETION_ONSTACK(wait); struct adc_chan_result adc_result = { .physical = 0lu, }; int rc; #ifdef CONFIG_LGE_PM /* */ int wait_ret; #endif if (!chip || !temp) return -EINVAL; *temp = chip->temp; rc = adc_channel_request_conv(chip->adc_handle, &wait); if (rc < 0) { pr_err("%s: adc_channel_request_conv() failed, rc = %d\n", __func__, rc); return rc; } #ifdef CONFIG_LGE_PM /* */ wait_ret = wait_for_completion_timeout(&wait, msecs_to_jiffies(MSM_PMIC_ADC_READ_TIMEOUT)); if(wait_ret <= 0) { printk(KERN_ERR "===%s: failed to adc wait for completion!===\n",__func__); goto sanity_out; } #else wait_for_completion(&wait); #endif rc = adc_channel_read_result(chip->adc_handle, &adc_result); if (rc < 0) { pr_err("%s: adc_channel_read_result() failed, rc = %d\n", __func__, rc); return rc; } *temp = adc_result.physical; chip->temp = adc_result.physical; #if defined(CONFIG_MACH_LGE_325_BOARD_SKT) || defined(CONFIG_MACH_LGE_325_BOARD_LGU) /* */ if (chip->temp > 46000) { printk("%s: pmic_die_temp = %ld temp_range = %d \n", __func__, *temp, pmic_die_temp_range); pmic_die_temp_range = 3; } else if (chip->temp > 44000) pmic_die_temp_range = 2; else if (chip->temp > 39000) pmic_die_temp_range = 1; else pmic_die_temp_range = 0; #endif return 0; #ifdef CONFIG_LGE_PM /* */ sanity_out: pm8058_xoadc_clear_recentQ(); *temp = MSM_CHARGER_GAUGE_MISSING_TEMP; chip->temp = MSM_CHARGER_GAUGE_MISSING_TEMP; printk(KERN_ERR "============== batt temp adc read fail so default temp ===============\n"); return 0; #endif } static int pm8xxx_tz_get_temp_pm8xxx_adc(struct thermal_zone_device *thermal, unsigned long *temp) { struct pm8xxx_tm_chip *chip = thermal->devdata; struct pm8xxx_adc_chan_result result = { .physical = 0lu, }; int rc; if (!chip || !temp) return -EINVAL; *temp = chip->temp; rc = pm8xxx_adc_read(chip->cdata.adc_channel, &result); if (rc < 0) { pr_err("%s: adc_channel_read_result() failed, rc = %d\n", chip->cdata.tm_name, rc); return rc; } *temp = result.physical; chip->temp = result.physical; return 0; } static int pm8xxx_tz_get_mode(struct thermal_zone_device *thermal, enum thermal_device_mode *mode) { struct pm8xxx_tm_chip *chip = thermal->devdata; if (!chip || !mode) return -EINVAL; *mode = chip->mode; return 0; } static int pm8xxx_tz_set_mode(struct thermal_zone_device *thermal, enum thermal_device_mode mode) { struct pm8xxx_tm_chip *chip = thermal->devdata; if (!chip) return -EINVAL; if (mode != chip->mode) { if (mode == THERMAL_DEVICE_ENABLED) pm8xxx_tm_shutdown_override(chip, SOFTWARE_OVERRIDE_ENABLED); else pm8xxx_tm_shutdown_override(chip, SOFTWARE_OVERRIDE_DISABLED); } chip->mode = mode; return 0; } static int pm8xxx_tz_get_trip_type(struct thermal_zone_device *thermal, int trip, enum thermal_trip_type *type) { if (trip < 0 || !type) return -EINVAL; switch (trip) { case TRIP_STAGE3: *type = THERMAL_TRIP_CRITICAL; break; case TRIP_STAGE2: *type = THERMAL_TRIP_HOT; break; case TRIP_STAGE1: *type = THERMAL_TRIP_HOT; break; default: return -EINVAL; } return 0; } static int pm8xxx_tz_get_trip_temp(struct thermal_zone_device *thermal, int trip, unsigned long *temp) { struct pm8xxx_tm_chip *chip = thermal->devdata; int thresh_temp; if (!chip || trip < 0 || !temp) return -EINVAL; thresh_temp = chip->thresh * TEMP_THRESH_STEP + TEMP_THRESH_MIN; switch (trip) { case TRIP_STAGE3: thresh_temp += 2 * TEMP_STAGE_STEP; break; case TRIP_STAGE2: thresh_temp += TEMP_STAGE_STEP; break; case TRIP_STAGE1: break; default: return -EINVAL; } *temp = thresh_temp; return 0; } static int pm8xxx_tz_get_crit_temp(struct thermal_zone_device *thermal, unsigned long *temp) { struct pm8xxx_tm_chip *chip = thermal->devdata; if (!chip || !temp) return -EINVAL; *temp = chip->thresh * TEMP_THRESH_STEP + TEMP_THRESH_MIN + 2 * TEMP_STAGE_STEP; return 0; } static struct thermal_zone_device_ops pm8xxx_thermal_zone_ops_no_adc = { .get_temp = pm8xxx_tz_get_temp_no_adc, .get_mode = pm8xxx_tz_get_mode, .set_mode = pm8xxx_tz_set_mode, .get_trip_type = pm8xxx_tz_get_trip_type, .get_trip_temp = pm8xxx_tz_get_trip_temp, .get_crit_temp = pm8xxx_tz_get_crit_temp, }; static struct thermal_zone_device_ops pm8xxx_thermal_zone_ops_pm8xxx_adc = { .get_temp = pm8xxx_tz_get_temp_pm8xxx_adc, .get_mode = pm8xxx_tz_get_mode, .set_mode = pm8xxx_tz_set_mode, .get_trip_type = pm8xxx_tz_get_trip_type, .get_trip_temp = pm8xxx_tz_get_trip_temp, .get_crit_temp = pm8xxx_tz_get_crit_temp, }; static struct thermal_zone_device_ops pm8xxx_thermal_zone_ops_pm8058_adc = { .get_temp = pm8xxx_tz_get_temp_pm8058_adc, .get_mode = pm8xxx_tz_get_mode, .set_mode = pm8xxx_tz_set_mode, .get_trip_type = pm8xxx_tz_get_trip_type, .get_trip_temp = pm8xxx_tz_get_trip_temp, .get_crit_temp = pm8xxx_tz_get_crit_temp, }; static void pm8xxx_tm_work(struct work_struct *work) { struct pm8xxx_tm_chip *chip = container_of(work, struct pm8xxx_tm_chip, irq_work); int rc; u8 reg; rc = pm8xxx_tm_read_ctrl(chip, ®); if (rc < 0) goto bail; if (chip->cdata.adc_type == PM8XXX_TM_ADC_NONE) { rc = pm8xxx_tm_update_temp_no_adc(chip); if (rc < 0) goto bail; pr_info("%s: Temp Alarm - stage=%u, threshold=%u, " "temp=%lu mC\n", chip->cdata.tm_name, chip->stage, chip->thresh, chip->temp); } else { chip->stage = (reg & TEMP_ALARM_CTRL_STATUS_MASK) >> TEMP_ALARM_CTRL_STATUS_SHIFT; chip->thresh = (reg & TEMP_ALARM_CTRL_THRESH_MASK) >> TEMP_ALARM_CTRL_THRESH_SHIFT; pr_info("%s: Temp Alarm - stage=%u, threshold=%u\n", chip->cdata.tm_name, chip->stage, chip->thresh); } /* Clear status bits. */ if (reg & (TEMP_ALARM_CTRL_ST2_SD | TEMP_ALARM_CTRL_ST3_SD)) { reg &= ~(TEMP_ALARM_CTRL_ST2_SD | TEMP_ALARM_CTRL_ST3_SD | TEMP_ALARM_CTRL_STATUS_MASK); pm8xxx_tm_write_ctrl(chip, reg); } thermal_zone_device_update(chip->tz_dev); /* Notify user space */ if (chip->mode == THERMAL_DEVICE_ENABLED) kobject_uevent(&chip->tz_dev->device.kobj, KOBJ_CHANGE); bail: enable_irq(chip->tempstat_irq); enable_irq(chip->overtemp_irq); } static irqreturn_t pm8xxx_tm_isr(int irq, void *data) { struct pm8xxx_tm_chip *chip = data; disable_irq_nosync(chip->tempstat_irq); disable_irq_nosync(chip->overtemp_irq); schedule_work(&chip->irq_work); return IRQ_HANDLED; } static int pm8xxx_tm_init_reg(struct pm8xxx_tm_chip *chip) { int rc; u8 reg; rc = pm8xxx_tm_read_ctrl(chip, ®); if (rc < 0) return rc; chip->stage = (reg & TEMP_ALARM_CTRL_STATUS_MASK) >> TEMP_ALARM_CTRL_STATUS_SHIFT; chip->temp = 0; /* Use temperature threshold set 0: (105, 125, 145) */ chip->thresh = 0; reg = (chip->thresh << TEMP_ALARM_CTRL_THRESH_SHIFT) & TEMP_ALARM_CTRL_THRESH_MASK; rc = pm8xxx_tm_write_ctrl(chip, reg); if (rc < 0) return rc; /* * Set the PMIC alarm module PWM to have a frequency of 8 Hz. This * helps cut down on the number of unnecessary interrupts fired when * changing between thermal stages. Also, Enable the over temperature * PWM whenever the PMIC is enabled. */ reg = (1 << TEMP_ALARM_PWM_EN_SHIFT) | (3 << TEMP_ALARM_PWM_PER_PRE_SHIFT) | (3 << TEMP_ALARM_PWM_PER_DIV_SHIFT); rc = pm8xxx_tm_write_pwm(chip, reg); return rc; } static int pm8xxx_init_adc(struct pm8xxx_tm_chip *chip, bool enable) { int rc = 0; if (chip->cdata.adc_type == PM8XXX_TM_ADC_PM8058_ADC) { if (enable) { rc = adc_channel_open(chip->cdata.adc_channel, &(chip->adc_handle)); if (rc < 0) pr_err("adc_channel_open() failed.\n"); } else { adc_channel_close(chip->adc_handle); } } return rc; }