ab8500.c 5.4 KB

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