ab8500.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright (C) ST-Ericsson 2010 - 2013
  3. * Author: Martin Persson <martin.persson@stericsson.com>
  4. * Hongbo Zhang <hongbo.zhang@linaro.org>
  5. * License Terms: GNU General Public License v2
  6. *
  7. * When the AB8500 thermal warning temperature is reached (threshold cannot
  8. * be changed by SW), an interrupt is set, and if no further action is taken
  9. * within a certain time frame, pm_power off will be called.
  10. *
  11. * When AB8500 thermal shutdown temperature is reached a hardware shutdown of
  12. * the AB8500 will occur.
  13. */
  14. #include <linux/err.h>
  15. #include <linux/hwmon.h>
  16. #include <linux/hwmon-sysfs.h>
  17. #include <linux/mfd/abx500.h>
  18. #include <linux/mfd/abx500/ab8500-bm.h>
  19. #include <linux/mfd/abx500/ab8500-gpadc.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/power/ab8500.h>
  23. #include <linux/slab.h>
  24. #include <linux/sysfs.h>
  25. #include "abx500.h"
  26. #define DEFAULT_POWER_OFF_DELAY (HZ * 10)
  27. #define THERMAL_VCC 1800
  28. #define PULL_UP_RESISTOR 47000
  29. /* Number of monitored sensors should not greater than NUM_SENSORS */
  30. #define NUM_MONITORED_SENSORS 4
  31. struct ab8500_gpadc_cfg {
  32. const struct abx500_res_to_temp *temp_tbl;
  33. int tbl_sz;
  34. int vcc;
  35. int r_up;
  36. };
  37. struct ab8500_temp {
  38. struct ab8500_gpadc *gpadc;
  39. struct ab8500_btemp *btemp;
  40. struct delayed_work power_off_work;
  41. struct ab8500_gpadc_cfg cfg;
  42. struct abx500_temp *abx500_data;
  43. };
  44. /*
  45. * The hardware connection is like this:
  46. * VCC----[ R_up ]-----[ NTC ]----GND
  47. * where R_up is pull-up resistance, and GPADC measures voltage on NTC.
  48. * and res_to_temp table is strictly sorted by falling resistance values.
  49. */
  50. static int ab8500_voltage_to_temp(struct ab8500_gpadc_cfg *cfg,
  51. int v_ntc, int *temp)
  52. {
  53. int r_ntc, i = 0, tbl_sz = cfg->tbl_sz;
  54. const struct abx500_res_to_temp *tbl = cfg->temp_tbl;
  55. if (cfg->vcc < 0 || v_ntc >= cfg->vcc)
  56. return -EINVAL;
  57. r_ntc = v_ntc * cfg->r_up / (cfg->vcc - v_ntc);
  58. if (r_ntc > tbl[0].resist || r_ntc < tbl[tbl_sz - 1].resist)
  59. return -EINVAL;
  60. while (!(r_ntc <= tbl[i].resist && r_ntc > tbl[i + 1].resist) &&
  61. i < tbl_sz - 2)
  62. i++;
  63. /* return milli-Celsius */
  64. *temp = tbl[i].temp * 1000 + ((tbl[i + 1].temp - tbl[i].temp) * 1000 *
  65. (r_ntc - tbl[i].resist)) / (tbl[i + 1].resist - tbl[i].resist);
  66. return 0;
  67. }
  68. static int ab8500_read_sensor(struct abx500_temp *data, u8 sensor, int *temp)
  69. {
  70. int voltage, ret;
  71. struct ab8500_temp *ab8500_data = data->plat_data;
  72. if (sensor == BAT_CTRL) {
  73. *temp = ab8500_btemp_get_batctrl_temp(ab8500_data->btemp);
  74. } else if (sensor == BTEMP_BALL) {
  75. *temp = ab8500_btemp_get_temp(ab8500_data->btemp);
  76. } else {
  77. voltage = ab8500_gpadc_convert(ab8500_data->gpadc, sensor);
  78. if (voltage < 0)
  79. return voltage;
  80. ret = ab8500_voltage_to_temp(&ab8500_data->cfg, voltage, temp);
  81. if (ret < 0)
  82. return ret;
  83. }
  84. return 0;
  85. }
  86. static void ab8500_thermal_power_off(struct work_struct *work)
  87. {
  88. struct ab8500_temp *ab8500_data = container_of(work,
  89. struct ab8500_temp, power_off_work.work);
  90. struct abx500_temp *abx500_data = ab8500_data->abx500_data;
  91. dev_warn(&abx500_data->pdev->dev, "Power off due to critical temp\n");
  92. pm_power_off();
  93. }
  94. static ssize_t ab8500_show_name(struct device *dev,
  95. struct device_attribute *devattr, char *buf)
  96. {
  97. return sprintf(buf, "ab8500\n");
  98. }
  99. static ssize_t ab8500_show_label(struct device *dev,
  100. struct device_attribute *devattr, char *buf)
  101. {
  102. char *label;
  103. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  104. int index = attr->index;
  105. switch (index) {
  106. case 1:
  107. label = "ext_adc1";
  108. break;
  109. case 2:
  110. label = "ext_adc2";
  111. break;
  112. case 3:
  113. label = "bat_temp";
  114. break;
  115. case 4:
  116. label = "bat_ctrl";
  117. break;
  118. default:
  119. return -EINVAL;
  120. }
  121. return sprintf(buf, "%s\n", label);
  122. }
  123. static int ab8500_temp_irq_handler(int irq, struct abx500_temp *data)
  124. {
  125. struct ab8500_temp *ab8500_data = data->plat_data;
  126. dev_warn(&data->pdev->dev, "Power off in %d s\n",
  127. DEFAULT_POWER_OFF_DELAY / HZ);
  128. schedule_delayed_work(&ab8500_data->power_off_work,
  129. DEFAULT_POWER_OFF_DELAY);
  130. return 0;
  131. }
  132. int abx500_hwmon_init(struct abx500_temp *data)
  133. {
  134. struct ab8500_temp *ab8500_data;
  135. ab8500_data = devm_kzalloc(&data->pdev->dev, sizeof(*ab8500_data),
  136. GFP_KERNEL);
  137. if (!ab8500_data)
  138. return -ENOMEM;
  139. ab8500_data->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
  140. if (IS_ERR(ab8500_data->gpadc))
  141. return PTR_ERR(ab8500_data->gpadc);
  142. ab8500_data->btemp = ab8500_btemp_get();
  143. if (IS_ERR(ab8500_data->btemp))
  144. return PTR_ERR(ab8500_data->btemp);
  145. INIT_DELAYED_WORK(&ab8500_data->power_off_work,
  146. ab8500_thermal_power_off);
  147. ab8500_data->cfg.vcc = THERMAL_VCC;
  148. ab8500_data->cfg.r_up = PULL_UP_RESISTOR;
  149. ab8500_data->cfg.temp_tbl = ab8500_temp_tbl_a_thermistor;
  150. ab8500_data->cfg.tbl_sz = ab8500_temp_tbl_a_size;
  151. data->plat_data = ab8500_data;
  152. /*
  153. * ADC_AUX1 and ADC_AUX2, connected to external NTC
  154. * BTEMP_BALL and BAT_CTRL, fixed usage
  155. */
  156. data->gpadc_addr[0] = ADC_AUX1;
  157. data->gpadc_addr[1] = ADC_AUX2;
  158. data->gpadc_addr[2] = BTEMP_BALL;
  159. data->gpadc_addr[3] = BAT_CTRL;
  160. data->monitored_sensors = NUM_MONITORED_SENSORS;
  161. data->ops.read_sensor = ab8500_read_sensor;
  162. data->ops.irq_handler = ab8500_temp_irq_handler;
  163. data->ops.show_name = ab8500_show_name;
  164. data->ops.show_label = ab8500_show_label;
  165. data->ops.is_visible = NULL;
  166. return 0;
  167. }
  168. EXPORT_SYMBOL(abx500_hwmon_init);
  169. MODULE_AUTHOR("Hongbo Zhang <hongbo.zhang@linaro.org>");
  170. MODULE_DESCRIPTION("AB8500 temperature driver");
  171. MODULE_LICENSE("GPL");