/drivers/platform/x86/intel_mid_thermal.c

http://github.com/mirrors/linux · C · 554 lines · 307 code · 77 blank · 170 comment · 54 complexity · 7f47ed7044955e92ab07ec1829711aa7 MD5 · raw file

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