2 * intel_mid_thermal.c - Intel MID platform thermal driver
4 * Copyright (C) 2011 Intel Corporation
6 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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.
21 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22 * Author: Durgadoss R <durgadoss.r@intel.com>
25 #define pr_fmt(fmt) "intel_mid_thermal: " fmt
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/err.h>
30 #include <linux/param.h>
31 #include <linux/device.h>
32 #include <linux/platform_device.h>
33 #include <linux/slab.h>
35 #include <linux/thermal.h>
37 #include <asm/intel_scu_ipc.h>
39 /* Number of thermal sensors */
40 #define MSIC_THERMAL_SENSORS 4
42 /* ADC1 - thermal registers */
43 #define MSIC_THERM_ADC1CNTL1 0x1C0
44 #define MSIC_ADC_ENBL 0x10
45 #define MSIC_ADC_START 0x08
47 #define MSIC_THERM_ADC1CNTL3 0x1C2
48 #define MSIC_ADCTHERM_ENBL 0x04
49 #define MSIC_ADCRRDATA_ENBL 0x05
50 #define MSIC_CHANL_MASK_VAL 0x0F
52 #define MSIC_STOPBIT_MASK 16
53 #define MSIC_ADCTHERM_MASK 4
54 /* Number of ADC channels */
55 #define ADC_CHANLS_MAX 15
56 #define ADC_LOOP_MAX (ADC_CHANLS_MAX - MSIC_THERMAL_SENSORS)
58 /* ADC channel code values */
59 #define SKIN_SENSOR0_CODE 0x08
60 #define SKIN_SENSOR1_CODE 0x09
61 #define SYS_SENSOR_CODE 0x0A
62 #define MSIC_DIE_SENSOR_CODE 0x03
64 #define SKIN_THERM_SENSOR0 0
65 #define SKIN_THERM_SENSOR1 1
66 #define SYS_THERM_SENSOR2 2
67 #define MSIC_DIE_THERM_SENSOR3 3
73 #define ADC_VAL20C 720
74 #define ADC_VAL40C 508
75 #define ADC_VAL60C 315
77 /* ADC base addresses */
78 #define ADC_CHNL_START_ADDR 0x1C5 /* increments by 1 */
79 #define ADC_DATA_START_ADDR 0x1D4 /* increments by 2 */
81 /* MSIC die attributes */
82 #define MSIC_DIE_ADC_MIN 488
83 #define MSIC_DIE_ADC_MAX 1004
85 /* This holds the address of the first free ADC channel,
86 * among the 15 channels
88 static int channel_index
;
90 struct platform_info
{
91 struct platform_device
*pdev
;
92 struct thermal_zone_device
*tzd
[MSIC_THERMAL_SENSORS
];
95 struct thermal_device_info
{
96 unsigned int chnl_addr
;
98 /* This holds the current temperature in millidegree celsius */
103 * to_msic_die_temp - converts adc_val to msic_die temperature
104 * @adc_val: ADC value to be converted
108 static int to_msic_die_temp(uint16_t adc_val
)
110 return (368 * (adc_val
) / 1000) - 220;
114 * is_valid_adc - checks whether the adc code is within the defined range
115 * @min: minimum value for the sensor
116 * @max: maximum value for the sensor
120 static int is_valid_adc(uint16_t adc_val
, uint16_t min
, uint16_t max
)
122 return (adc_val
>= min
) && (adc_val
<= max
);
126 * adc_to_temp - converts the ADC code to temperature in C
127 * @direct: true if ths channel is direct index
128 * @adc_val: the adc_val that needs to be converted
129 * @tp: temperature return value
131 * Linear approximation is used to covert the skin adc value into temperature.
132 * This technique is used to avoid very long look-up table to get
133 * the appropriate temp value from ADC value.
134 * The adc code vs sensor temp curve is split into five parts
135 * to achieve very close approximate temp value with less than
138 static int adc_to_temp(int direct
, uint16_t adc_val
, unsigned long *tp
)
142 /* Direct conversion for die temperature */
144 if (is_valid_adc(adc_val
, MSIC_DIE_ADC_MIN
, MSIC_DIE_ADC_MAX
)) {
145 *tp
= to_msic_die_temp(adc_val
) * 1000;
151 if (!is_valid_adc(adc_val
, ADC_MIN
, ADC_MAX
))
154 /* Linear approximation for skin temperature */
155 if (adc_val
> ADC_VAL0C
)
156 temp
= 177 - (adc_val
/5);
157 else if ((adc_val
<= ADC_VAL0C
) && (adc_val
> ADC_VAL20C
))
158 temp
= 111 - (adc_val
/8);
159 else if ((adc_val
<= ADC_VAL20C
) && (adc_val
> ADC_VAL40C
))
160 temp
= 92 - (adc_val
/10);
161 else if ((adc_val
<= ADC_VAL40C
) && (adc_val
> ADC_VAL60C
))
162 temp
= 91 - (adc_val
/10);
164 temp
= 112 - (adc_val
/6);
166 /* Convert temperature in celsius to milli degree celsius */
172 * mid_read_temp - read sensors for temperature
173 * @temp: holds the current temperature for the sensor after reading
175 * reads the adc_code from the channel and converts it to real
176 * temperature. The converted value is stored in temp.
180 static int mid_read_temp(struct thermal_zone_device
*tzd
, unsigned long *temp
)
182 struct thermal_device_info
*td_info
= tzd
->devdata
;
183 uint16_t adc_val
, addr
;
186 unsigned long curr_temp
;
189 addr
= td_info
->chnl_addr
;
191 /* Enable the msic for conversion before reading */
192 ret
= intel_scu_ipc_iowrite8(MSIC_THERM_ADC1CNTL3
, MSIC_ADCRRDATA_ENBL
);
196 /* Re-toggle the RRDATARD bit (temporary workaround) */
197 ret
= intel_scu_ipc_iowrite8(MSIC_THERM_ADC1CNTL3
, MSIC_ADCTHERM_ENBL
);
201 /* Read the higher bits of data */
202 ret
= intel_scu_ipc_ioread8(addr
, &data
);
206 /* Shift bits to accommodate the lower two data bits */
207 adc_val
= (data
<< 2);
210 ret
= intel_scu_ipc_ioread8(addr
, &data
);/* Read lower bits */
214 /* Adding lower two bits to the higher bits */
218 /* Convert ADC value to temperature */
219 ret
= adc_to_temp(td_info
->direct
, adc_val
, &curr_temp
);
221 *temp
= td_info
->curr_temp
= curr_temp
;
226 * configure_adc - enables/disables the ADC for conversion
227 * @val: zero: disables the ADC non-zero:enables the ADC
229 * Enable/Disable the ADC depending on the argument
233 static int configure_adc(int val
)
238 ret
= intel_scu_ipc_ioread8(MSIC_THERM_ADC1CNTL1
, &data
);
243 /* Enable and start the ADC */
244 data
|= (MSIC_ADC_ENBL
| MSIC_ADC_START
);
246 /* Just stop the ADC */
247 data
&= (~MSIC_ADC_START
);
249 return intel_scu_ipc_iowrite8(MSIC_THERM_ADC1CNTL1
, data
);
253 * set_up_therm_channel - enable thermal channel for conversion
254 * @base_addr: index of free msic ADC channel
256 * Enable all the three channels for conversion
260 static int set_up_therm_channel(u16 base_addr
)
264 /* Enable all the sensor channels */
265 ret
= intel_scu_ipc_iowrite8(base_addr
, SKIN_SENSOR0_CODE
);
269 ret
= intel_scu_ipc_iowrite8(base_addr
+ 1, SKIN_SENSOR1_CODE
);
273 ret
= intel_scu_ipc_iowrite8(base_addr
+ 2, SYS_SENSOR_CODE
);
277 /* Since this is the last channel, set the stop bit
278 * to 1 by ORing the DIE_SENSOR_CODE with 0x10 */
279 ret
= intel_scu_ipc_iowrite8(base_addr
+ 3,
280 (MSIC_DIE_SENSOR_CODE
| 0x10));
284 /* Enable ADC and start it */
285 return configure_adc(1);
289 * reset_stopbit - sets the stop bit to 0 on the given channel
290 * @addr: address of the channel
294 static int reset_stopbit(uint16_t addr
)
298 ret
= intel_scu_ipc_ioread8(addr
, &data
);
301 /* Set the stop bit to zero */
302 return intel_scu_ipc_iowrite8(addr
, (data
& 0xEF));
306 * find_free_channel - finds an empty channel for conversion
308 * If the ADC is not enabled then start using 0th channel
309 * itself. Otherwise find an empty channel by looking for a
310 * channel in which the stopbit is set to 1. returns the index
311 * of the first free channel if succeeds or an error code.
315 * FIXME: Ultimately the channel allocator will move into the intel_scu_ipc
318 static int find_free_channel(void)
324 /* check whether ADC is enabled */
325 ret
= intel_scu_ipc_ioread8(MSIC_THERM_ADC1CNTL1
, &data
);
329 if ((data
& MSIC_ADC_ENBL
) == 0)
332 /* ADC is already enabled; Looking for an empty channel */
333 for (i
= 0; i
< ADC_CHANLS_MAX
; i
++) {
334 ret
= intel_scu_ipc_ioread8(ADC_CHNL_START_ADDR
+ i
, &data
);
338 if (data
& MSIC_STOPBIT_MASK
) {
343 return (ret
> ADC_LOOP_MAX
) ? (-EINVAL
) : ret
;
347 * mid_initialize_adc - initializing the ADC
348 * @dev: our device structure
350 * Initialize the ADC for reading thermistor values. Can sleep.
352 static int mid_initialize_adc(struct device
*dev
)
359 * Ensure that adctherm is disabled before we
362 ret
= intel_scu_ipc_ioread8(MSIC_THERM_ADC1CNTL3
, &data
);
366 if (data
& MSIC_ADCTHERM_MASK
)
367 dev_warn(dev
, "ADCTHERM already set");
369 /* Index of the first channel in which the stop bit is set */
370 channel_index
= find_free_channel();
371 if (channel_index
< 0) {
372 dev_err(dev
, "No free ADC channels");
373 return channel_index
;
376 base_addr
= ADC_CHNL_START_ADDR
+ channel_index
;
378 if (!(channel_index
== 0 || channel_index
== ADC_LOOP_MAX
)) {
379 /* Reset stop bit for channels other than 0 and 12 */
380 ret
= reset_stopbit(base_addr
);
384 /* Index of the first free channel */
389 ret
= set_up_therm_channel(base_addr
);
391 dev_err(dev
, "unable to enable ADC");
394 dev_dbg(dev
, "ADC initialization successful");
399 * initialize_sensor - sets default temp and timer ranges
400 * @index: index of the sensor
404 static struct thermal_device_info
*initialize_sensor(int index
)
406 struct thermal_device_info
*td_info
=
407 kzalloc(sizeof(struct thermal_device_info
), GFP_KERNEL
);
412 /* Set the base addr of the channel for this sensor */
413 td_info
->chnl_addr
= ADC_DATA_START_ADDR
+ 2 * (channel_index
+ index
);
414 /* Sensor 3 is direct conversion */
421 * mid_thermal_resume - resume routine
422 * @pdev: platform device structure
424 * mid thermal resume: re-initializes the adc. Can sleep.
426 static int mid_thermal_resume(struct platform_device
*pdev
)
428 return mid_initialize_adc(&pdev
->dev
);
432 * mid_thermal_suspend - suspend routine
433 * @pdev: platform device structure
435 * mid thermal suspend implements the suspend functionality
436 * by stopping the ADC. Can sleep.
438 static int mid_thermal_suspend(struct platform_device
*pdev
, pm_message_t mesg
)
441 * This just stops the ADC and does not disable it.
442 * temporary workaround until we have a generic ADC driver.
443 * If 0 is passed, it disables the ADC.
445 return configure_adc(0);
449 * read_curr_temp - reads the current temperature and stores in temp
450 * @temp: holds the current temperature value after reading
454 static int read_curr_temp(struct thermal_zone_device
*tzd
, unsigned long *temp
)
456 WARN_ON(tzd
== NULL
);
457 return mid_read_temp(tzd
, temp
);
461 static struct thermal_zone_device_ops tzd_ops
= {
462 .get_temp
= read_curr_temp
,
466 * mid_thermal_probe - mfld thermal initialize
467 * @pdev: platform device structure
469 * mid thermal probe initializes the hardware and registers
470 * all the sensors with the generic thermal framework. Can sleep.
472 static int mid_thermal_probe(struct platform_device
*pdev
)
474 static char *name
[MSIC_THERMAL_SENSORS
] = {
475 "skin0", "skin1", "sys", "msicdie"
480 struct platform_info
*pinfo
;
482 pinfo
= kzalloc(sizeof(struct platform_info
), GFP_KERNEL
);
486 /* Initializing the hardware */
487 ret
= mid_initialize_adc(&pdev
->dev
);
489 dev_err(&pdev
->dev
, "ADC init failed");
494 /* Register each sensor with the generic thermal framework*/
495 for (i
= 0; i
< MSIC_THERMAL_SENSORS
; i
++) {
496 struct thermal_device_info
*td_info
= initialize_sensor(i
);
502 pinfo
->tzd
[i
] = thermal_zone_device_register(name
[i
],
503 0, td_info
, &tzd_ops
, 0, 0, 0, 0);
504 if (IS_ERR(pinfo
->tzd
[i
])) {
506 ret
= PTR_ERR(pinfo
->tzd
[i
]);
512 platform_set_drvdata(pdev
, pinfo
);
517 kfree(pinfo
->tzd
[i
]->devdata
);
518 thermal_zone_device_unregister(pinfo
->tzd
[i
]);
526 * mid_thermal_remove - mfld thermal finalize
527 * @dev: platform device structure
529 * MLFD thermal remove unregisters all the sensors from the generic
530 * thermal framework. Can sleep.
532 static int mid_thermal_remove(struct platform_device
*pdev
)
535 struct platform_info
*pinfo
= platform_get_drvdata(pdev
);
537 for (i
= 0; i
< MSIC_THERMAL_SENSORS
; i
++) {
538 kfree(pinfo
->tzd
[i
]->devdata
);
539 thermal_zone_device_unregister(pinfo
->tzd
[i
]);
543 platform_set_drvdata(pdev
, NULL
);
546 return configure_adc(0);
549 #define DRIVER_NAME "msic_sensor"
551 static const struct platform_device_id therm_id_table
[] = {
556 static struct platform_driver mid_thermal_driver
= {
559 .owner
= THIS_MODULE
,
561 .probe
= mid_thermal_probe
,
562 .suspend
= mid_thermal_suspend
,
563 .resume
= mid_thermal_resume
,
564 .remove
= __devexit_p(mid_thermal_remove
),
565 .id_table
= therm_id_table
,
568 static int __init
mid_thermal_module_init(void)
570 return platform_driver_register(&mid_thermal_driver
);
573 static void __exit
mid_thermal_module_exit(void)
575 platform_driver_unregister(&mid_thermal_driver
);
578 module_init(mid_thermal_module_init
);
579 module_exit(mid_thermal_module_exit
);
581 MODULE_AUTHOR("Durgadoss R <durgadoss.r@intel.com>");
582 MODULE_DESCRIPTION("Intel Medfield Platform Thermal Driver");
583 MODULE_LICENSE("GPL");