Merge tag 'mmc-v4.9-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/ulfh/mmc
[cascardo/linux.git] / drivers / thermal / qcom / tsens-8996.c
1 /*
2  * Copyright (c) 2015, The Linux Foundation. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 and
6  * only version 2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  */
14
15 #include <linux/platform_device.h>
16 #include <linux/regmap.h>
17 #include "tsens.h"
18
19 #define STATUS_OFFSET   0x10a0
20 #define LAST_TEMP_MASK  0xfff
21 #define STATUS_VALID_BIT        BIT(21)
22 #define CODE_SIGN_BIT           BIT(11)
23
24 static int get_temp_8996(struct tsens_device *tmdev, int id, int *temp)
25 {
26         struct tsens_sensor *s = &tmdev->sensor[id];
27         u32 code;
28         unsigned int sensor_addr;
29         int last_temp = 0, last_temp2 = 0, last_temp3 = 0, ret;
30
31         sensor_addr = STATUS_OFFSET + s->hw_id * 4;
32         ret = regmap_read(tmdev->map, sensor_addr, &code);
33         if (ret)
34                 return ret;
35         last_temp = code & LAST_TEMP_MASK;
36         if (code & STATUS_VALID_BIT)
37                 goto done;
38
39         /* Try a second time */
40         ret = regmap_read(tmdev->map, sensor_addr, &code);
41         if (ret)
42                 return ret;
43         if (code & STATUS_VALID_BIT) {
44                 last_temp = code & LAST_TEMP_MASK;
45                 goto done;
46         } else {
47                 last_temp2 = code & LAST_TEMP_MASK;
48         }
49
50         /* Try a third/last time */
51         ret = regmap_read(tmdev->map, sensor_addr, &code);
52         if (ret)
53                 return ret;
54         if (code & STATUS_VALID_BIT) {
55                 last_temp = code & LAST_TEMP_MASK;
56                 goto done;
57         } else {
58                 last_temp3 = code & LAST_TEMP_MASK;
59         }
60
61         if (last_temp == last_temp2)
62                 last_temp = last_temp2;
63         else if (last_temp2 == last_temp3)
64                 last_temp = last_temp3;
65 done:
66         /* Code sign bit is the sign extension for a negative value */
67         if (last_temp & CODE_SIGN_BIT)
68                 last_temp |= ~CODE_SIGN_BIT;
69
70         /* Temperatures are in deciCelicius */
71         *temp = last_temp * 100;
72
73         return 0;
74 }
75
76 static const struct tsens_ops ops_8996 = {
77         .init           = init_common,
78         .get_temp       = get_temp_8996,
79 };
80
81 const struct tsens_data data_8996 = {
82         .num_sensors    = 13,
83         .ops            = &ops_8996,
84 };