intel_mid_thermal.c 16 KB

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