intel_mid_thermal.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*
  2. * intel_mid_thermal.c - Intel MID platform thermal driver
  3. *
  4. * Copyright (C) 2011 Intel Corporation
  5. *
  6. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; version 2 of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  20. *
  21. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  22. * Author: Durgadoss R <durgadoss.r@intel.com>
  23. */
  24. #define pr_fmt(fmt) "intel_mid_thermal: " fmt
  25. #include <linux/module.h>
  26. #include <linux/init.h>
  27. #include <linux/err.h>
  28. #include <linux/param.h>
  29. #include <linux/device.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/slab.h>
  32. #include <linux/pm.h>
  33. #include <linux/thermal.h>
  34. #include <linux/mfd/intel_msic.h>
  35. /* Number of thermal sensors */
  36. #define MSIC_THERMAL_SENSORS 4
  37. /* ADC1 - thermal registers */
  38. #define MSIC_ADC_ENBL 0x10
  39. #define MSIC_ADC_START 0x08
  40. #define MSIC_ADCTHERM_ENBL 0x04
  41. #define MSIC_ADCRRDATA_ENBL 0x05
  42. #define MSIC_CHANL_MASK_VAL 0x0F
  43. #define MSIC_STOPBIT_MASK 16
  44. #define MSIC_ADCTHERM_MASK 4
  45. /* Number of ADC channels */
  46. #define ADC_CHANLS_MAX 15
  47. #define ADC_LOOP_MAX (ADC_CHANLS_MAX - MSIC_THERMAL_SENSORS)
  48. /* ADC channel code values */
  49. #define SKIN_SENSOR0_CODE 0x08
  50. #define SKIN_SENSOR1_CODE 0x09
  51. #define SYS_SENSOR_CODE 0x0A
  52. #define MSIC_DIE_SENSOR_CODE 0x03
  53. #define SKIN_THERM_SENSOR0 0
  54. #define SKIN_THERM_SENSOR1 1
  55. #define SYS_THERM_SENSOR2 2
  56. #define MSIC_DIE_THERM_SENSOR3 3
  57. /* ADC code range */
  58. #define ADC_MAX 977
  59. #define ADC_MIN 162
  60. #define ADC_VAL0C 887
  61. #define ADC_VAL20C 720
  62. #define ADC_VAL40C 508
  63. #define ADC_VAL60C 315
  64. /* ADC base addresses */
  65. #define ADC_CHNL_START_ADDR INTEL_MSIC_ADC1ADDR0 /* increments by 1 */
  66. #define ADC_DATA_START_ADDR INTEL_MSIC_ADC1SNS0H /* increments by 2 */
  67. /* MSIC die attributes */
  68. #define MSIC_DIE_ADC_MIN 488
  69. #define MSIC_DIE_ADC_MAX 1004
  70. /* This holds the address of the first free ADC channel,
  71. * among the 15 channels
  72. */
  73. static int channel_index;
  74. struct platform_info {
  75. struct platform_device *pdev;
  76. struct thermal_zone_device *tzd[MSIC_THERMAL_SENSORS];
  77. };
  78. struct thermal_device_info {
  79. unsigned int chnl_addr;
  80. int direct;
  81. /* This holds the current temperature in millidegree celsius */
  82. long curr_temp;
  83. };
  84. /**
  85. * to_msic_die_temp - converts adc_val to msic_die temperature
  86. * @adc_val: ADC value to be converted
  87. *
  88. * Can sleep
  89. */
  90. static int to_msic_die_temp(uint16_t adc_val)
  91. {
  92. return (368 * (adc_val) / 1000) - 220;
  93. }
  94. /**
  95. * is_valid_adc - checks whether the adc code is within the defined range
  96. * @min: minimum value for the sensor
  97. * @max: maximum value for the sensor
  98. *
  99. * Can sleep
  100. */
  101. static int is_valid_adc(uint16_t adc_val, uint16_t min, uint16_t max)
  102. {
  103. return (adc_val >= min) && (adc_val <= max);
  104. }
  105. /**
  106. * adc_to_temp - converts the ADC code to temperature in C
  107. * @direct: true if ths channel is direct index
  108. * @adc_val: the adc_val that needs to be converted
  109. * @tp: temperature return value
  110. *
  111. * Linear approximation is used to covert the skin adc value into temperature.
  112. * This technique is used to avoid very long look-up table to get
  113. * the appropriate temp value from ADC value.
  114. * The adc code vs sensor temp curve is split into five parts
  115. * to achieve very close approximate temp value with less than
  116. * 0.5C error
  117. */
  118. static int adc_to_temp(int direct, uint16_t adc_val, int *tp)
  119. {
  120. int temp;
  121. /* Direct conversion for die temperature */
  122. if (direct) {
  123. if (is_valid_adc(adc_val, MSIC_DIE_ADC_MIN, MSIC_DIE_ADC_MAX)) {
  124. *tp = to_msic_die_temp(adc_val) * 1000;
  125. return 0;
  126. }
  127. return -ERANGE;
  128. }
  129. if (!is_valid_adc(adc_val, ADC_MIN, ADC_MAX))
  130. return -ERANGE;
  131. /* Linear approximation for skin temperature */
  132. if (adc_val > ADC_VAL0C)
  133. temp = 177 - (adc_val/5);
  134. else if ((adc_val <= ADC_VAL0C) && (adc_val > ADC_VAL20C))
  135. temp = 111 - (adc_val/8);
  136. else if ((adc_val <= ADC_VAL20C) && (adc_val > ADC_VAL40C))
  137. temp = 92 - (adc_val/10);
  138. else if ((adc_val <= ADC_VAL40C) && (adc_val > ADC_VAL60C))
  139. temp = 91 - (adc_val/10);
  140. else
  141. temp = 112 - (adc_val/6);
  142. /* Convert temperature in celsius to milli degree celsius */
  143. *tp = temp * 1000;
  144. return 0;
  145. }
  146. /**
  147. * mid_read_temp - read sensors for temperature
  148. * @temp: holds the current temperature for the sensor after reading
  149. *
  150. * reads the adc_code from the channel and converts it to real
  151. * temperature. The converted value is stored in temp.
  152. *
  153. * Can sleep
  154. */
  155. static int mid_read_temp(struct thermal_zone_device *tzd, int *temp)
  156. {
  157. struct thermal_device_info *td_info = tzd->devdata;
  158. uint16_t adc_val, addr;
  159. uint8_t data = 0;
  160. int ret;
  161. int curr_temp;
  162. addr = td_info->chnl_addr;
  163. /* Enable the msic for conversion before reading */
  164. ret = intel_msic_reg_write(INTEL_MSIC_ADC1CNTL3, MSIC_ADCRRDATA_ENBL);
  165. if (ret)
  166. return ret;
  167. /* Re-toggle the RRDATARD bit (temporary workaround) */
  168. ret = intel_msic_reg_write(INTEL_MSIC_ADC1CNTL3, MSIC_ADCTHERM_ENBL);
  169. if (ret)
  170. return ret;
  171. /* Read the higher bits of data */
  172. ret = intel_msic_reg_read(addr, &data);
  173. if (ret)
  174. return ret;
  175. /* Shift bits to accommodate the lower two data bits */
  176. adc_val = (data << 2);
  177. addr++;
  178. ret = intel_msic_reg_read(addr, &data);/* Read lower bits */
  179. if (ret)
  180. return ret;
  181. /* Adding lower two bits to the higher bits */
  182. data &= 03;
  183. adc_val += data;
  184. /* Convert ADC value to temperature */
  185. ret = adc_to_temp(td_info->direct, adc_val, &curr_temp);
  186. if (ret == 0)
  187. *temp = td_info->curr_temp = curr_temp;
  188. return ret;
  189. }
  190. /**
  191. * configure_adc - enables/disables the ADC for conversion
  192. * @val: zero: disables the ADC non-zero:enables the ADC
  193. *
  194. * Enable/Disable the ADC depending on the argument
  195. *
  196. * Can sleep
  197. */
  198. static int configure_adc(int val)
  199. {
  200. int ret;
  201. uint8_t data;
  202. ret = intel_msic_reg_read(INTEL_MSIC_ADC1CNTL1, &data);
  203. if (ret)
  204. return ret;
  205. if (val) {
  206. /* Enable and start the ADC */
  207. data |= (MSIC_ADC_ENBL | MSIC_ADC_START);
  208. } else {
  209. /* Just stop the ADC */
  210. data &= (~MSIC_ADC_START);
  211. }
  212. return intel_msic_reg_write(INTEL_MSIC_ADC1CNTL1, data);
  213. }
  214. /**
  215. * set_up_therm_channel - enable thermal channel for conversion
  216. * @base_addr: index of free msic ADC channel
  217. *
  218. * Enable all the three channels for conversion
  219. *
  220. * Can sleep
  221. */
  222. static int set_up_therm_channel(u16 base_addr)
  223. {
  224. int ret;
  225. /* Enable all the sensor channels */
  226. ret = intel_msic_reg_write(base_addr, SKIN_SENSOR0_CODE);
  227. if (ret)
  228. return ret;
  229. ret = intel_msic_reg_write(base_addr + 1, SKIN_SENSOR1_CODE);
  230. if (ret)
  231. return ret;
  232. ret = intel_msic_reg_write(base_addr + 2, SYS_SENSOR_CODE);
  233. if (ret)
  234. return ret;
  235. /* Since this is the last channel, set the stop bit
  236. * to 1 by ORing the DIE_SENSOR_CODE with 0x10 */
  237. ret = intel_msic_reg_write(base_addr + 3,
  238. (MSIC_DIE_SENSOR_CODE | 0x10));
  239. if (ret)
  240. return ret;
  241. /* Enable ADC and start it */
  242. return configure_adc(1);
  243. }
  244. /**
  245. * reset_stopbit - sets the stop bit to 0 on the given channel
  246. * @addr: address of the channel
  247. *
  248. * Can sleep
  249. */
  250. static int reset_stopbit(uint16_t addr)
  251. {
  252. int ret;
  253. uint8_t data;
  254. ret = intel_msic_reg_read(addr, &data);
  255. if (ret)
  256. return ret;
  257. /* Set the stop bit to zero */
  258. return intel_msic_reg_write(addr, (data & 0xEF));
  259. }
  260. /**
  261. * find_free_channel - finds an empty channel for conversion
  262. *
  263. * If the ADC is not enabled then start using 0th channel
  264. * itself. Otherwise find an empty channel by looking for a
  265. * channel in which the stopbit is set to 1. returns the index
  266. * of the first free channel if succeeds or an error code.
  267. *
  268. * Context: can sleep
  269. *
  270. * FIXME: Ultimately the channel allocator will move into the intel_scu_ipc
  271. * code.
  272. */
  273. static int find_free_channel(void)
  274. {
  275. int ret;
  276. int i;
  277. uint8_t data;
  278. /* check whether ADC is enabled */
  279. ret = intel_msic_reg_read(INTEL_MSIC_ADC1CNTL1, &data);
  280. if (ret)
  281. return ret;
  282. if ((data & MSIC_ADC_ENBL) == 0)
  283. return 0;
  284. /* ADC is already enabled; Looking for an empty channel */
  285. for (i = 0; i < ADC_CHANLS_MAX; i++) {
  286. ret = intel_msic_reg_read(ADC_CHNL_START_ADDR + i, &data);
  287. if (ret)
  288. return ret;
  289. if (data & MSIC_STOPBIT_MASK) {
  290. ret = i;
  291. break;
  292. }
  293. }
  294. return (ret > ADC_LOOP_MAX) ? (-EINVAL) : ret;
  295. }
  296. /**
  297. * mid_initialize_adc - initializing the ADC
  298. * @dev: our device structure
  299. *
  300. * Initialize the ADC for reading thermistor values. Can sleep.
  301. */
  302. static int mid_initialize_adc(struct device *dev)
  303. {
  304. u8 data;
  305. u16 base_addr;
  306. int ret;
  307. /*
  308. * Ensure that adctherm is disabled before we
  309. * initialize the ADC
  310. */
  311. ret = intel_msic_reg_read(INTEL_MSIC_ADC1CNTL3, &data);
  312. if (ret)
  313. return ret;
  314. data &= ~MSIC_ADCTHERM_MASK;
  315. ret = intel_msic_reg_write(INTEL_MSIC_ADC1CNTL3, data);
  316. if (ret)
  317. return ret;
  318. /* Index of the first channel in which the stop bit is set */
  319. channel_index = find_free_channel();
  320. if (channel_index < 0) {
  321. dev_err(dev, "No free ADC channels");
  322. return channel_index;
  323. }
  324. base_addr = ADC_CHNL_START_ADDR + channel_index;
  325. if (!(channel_index == 0 || channel_index == ADC_LOOP_MAX)) {
  326. /* Reset stop bit for channels other than 0 and 12 */
  327. ret = reset_stopbit(base_addr);
  328. if (ret)
  329. return ret;
  330. /* Index of the first free channel */
  331. base_addr++;
  332. channel_index++;
  333. }
  334. ret = set_up_therm_channel(base_addr);
  335. if (ret) {
  336. dev_err(dev, "unable to enable ADC");
  337. return ret;
  338. }
  339. dev_dbg(dev, "ADC initialization successful");
  340. return ret;
  341. }
  342. /**
  343. * initialize_sensor - sets default temp and timer ranges
  344. * @index: index of the sensor
  345. *
  346. * Context: can sleep
  347. */
  348. static struct thermal_device_info *initialize_sensor(int index)
  349. {
  350. struct thermal_device_info *td_info =
  351. kzalloc(sizeof(struct thermal_device_info), GFP_KERNEL);
  352. if (!td_info)
  353. return NULL;
  354. /* Set the base addr of the channel for this sensor */
  355. td_info->chnl_addr = ADC_DATA_START_ADDR + 2 * (channel_index + index);
  356. /* Sensor 3 is direct conversion */
  357. if (index == 3)
  358. td_info->direct = 1;
  359. return td_info;
  360. }
  361. #ifdef CONFIG_PM_SLEEP
  362. /**
  363. * mid_thermal_resume - resume routine
  364. * @dev: device structure
  365. *
  366. * mid thermal resume: re-initializes the adc. Can sleep.
  367. */
  368. static int mid_thermal_resume(struct device *dev)
  369. {
  370. return mid_initialize_adc(dev);
  371. }
  372. /**
  373. * mid_thermal_suspend - suspend routine
  374. * @dev: device structure
  375. *
  376. * mid thermal suspend implements the suspend functionality
  377. * by stopping the ADC. Can sleep.
  378. */
  379. static int mid_thermal_suspend(struct device *dev)
  380. {
  381. /*
  382. * This just stops the ADC and does not disable it.
  383. * temporary workaround until we have a generic ADC driver.
  384. * If 0 is passed, it disables the ADC.
  385. */
  386. return configure_adc(0);
  387. }
  388. #endif
  389. static SIMPLE_DEV_PM_OPS(mid_thermal_pm,
  390. mid_thermal_suspend, mid_thermal_resume);
  391. /**
  392. * read_curr_temp - reads the current temperature and stores in temp
  393. * @temp: holds the current temperature value after reading
  394. *
  395. * Can sleep
  396. */
  397. static int read_curr_temp(struct thermal_zone_device *tzd, int *temp)
  398. {
  399. WARN_ON(tzd == NULL);
  400. return mid_read_temp(tzd, temp);
  401. }
  402. /* Can't be const */
  403. static struct thermal_zone_device_ops tzd_ops = {
  404. .get_temp = read_curr_temp,
  405. };
  406. /**
  407. * mid_thermal_probe - mfld thermal initialize
  408. * @pdev: platform device structure
  409. *
  410. * mid thermal probe initializes the hardware and registers
  411. * all the sensors with the generic thermal framework. Can sleep.
  412. */
  413. static int mid_thermal_probe(struct platform_device *pdev)
  414. {
  415. static char *name[MSIC_THERMAL_SENSORS] = {
  416. "skin0", "skin1", "sys", "msicdie"
  417. };
  418. int ret;
  419. int i;
  420. struct platform_info *pinfo;
  421. pinfo = devm_kzalloc(&pdev->dev, sizeof(struct platform_info),
  422. GFP_KERNEL);
  423. if (!pinfo)
  424. return -ENOMEM;
  425. /* Initializing the hardware */
  426. ret = mid_initialize_adc(&pdev->dev);
  427. if (ret) {
  428. dev_err(&pdev->dev, "ADC init failed");
  429. return ret;
  430. }
  431. /* Register each sensor with the generic thermal framework*/
  432. for (i = 0; i < MSIC_THERMAL_SENSORS; i++) {
  433. struct thermal_device_info *td_info = initialize_sensor(i);
  434. if (!td_info) {
  435. ret = -ENOMEM;
  436. goto err;
  437. }
  438. pinfo->tzd[i] = thermal_zone_device_register(name[i],
  439. 0, 0, td_info, &tzd_ops, NULL, 0, 0);
  440. if (IS_ERR(pinfo->tzd[i])) {
  441. kfree(td_info);
  442. ret = PTR_ERR(pinfo->tzd[i]);
  443. goto err;
  444. }
  445. }
  446. pinfo->pdev = pdev;
  447. platform_set_drvdata(pdev, pinfo);
  448. return 0;
  449. err:
  450. while (--i >= 0) {
  451. kfree(pinfo->tzd[i]->devdata);
  452. thermal_zone_device_unregister(pinfo->tzd[i]);
  453. }
  454. configure_adc(0);
  455. return ret;
  456. }
  457. /**
  458. * mid_thermal_remove - mfld thermal finalize
  459. * @dev: platform device structure
  460. *
  461. * MLFD thermal remove unregisters all the sensors from the generic
  462. * thermal framework. Can sleep.
  463. */
  464. static int mid_thermal_remove(struct platform_device *pdev)
  465. {
  466. int i;
  467. struct platform_info *pinfo = platform_get_drvdata(pdev);
  468. for (i = 0; i < MSIC_THERMAL_SENSORS; i++) {
  469. kfree(pinfo->tzd[i]->devdata);
  470. thermal_zone_device_unregister(pinfo->tzd[i]);
  471. }
  472. /* Stop the ADC */
  473. return configure_adc(0);
  474. }
  475. #define DRIVER_NAME "msic_thermal"
  476. static const struct platform_device_id therm_id_table[] = {
  477. { DRIVER_NAME, 1 },
  478. { "msic_thermal", 1 },
  479. { }
  480. };
  481. MODULE_DEVICE_TABLE(platform, therm_id_table);
  482. static struct platform_driver mid_thermal_driver = {
  483. .driver = {
  484. .name = DRIVER_NAME,
  485. .pm = &mid_thermal_pm,
  486. },
  487. .probe = mid_thermal_probe,
  488. .remove = mid_thermal_remove,
  489. .id_table = therm_id_table,
  490. };
  491. module_platform_driver(mid_thermal_driver);
  492. MODULE_AUTHOR("Durgadoss R <durgadoss.r@intel.com>");
  493. MODULE_DESCRIPTION("Intel Medfield Platform Thermal Driver");
  494. MODULE_LICENSE("GPL");