intel_mid_thermal.c 14 KB

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