summaryrefstree
path: root/kernel/drivers/hwmon/sec_thermistor.c
blob: 5eca3dd2f6ffa4c3684f64299d79a5775c650bb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
 * sec_thermistor.c - SEC Thermistor
 *
 *  Copyright (c) 2013 Samsung Electronics Co., Ltd.
 *      http://www.samsung.com
 *  Minsung Kim <ms925.kim@samsung.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/err.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
#include <linux/iio/consumer.h>
#include <linux/platform_data/sec_thermistor.h>
#include <linux/sec_sysfs.h>

#define ADC_SAMPLING_CNT	5

struct sec_therm_info {
	int id;
	struct device *dev;
	struct device *sec_dev;
	struct device *hwmon_dev;
	struct sec_therm_platform_data *pdata;
	struct iio_channel *chan;
	char name[PLATFORM_NAME_SIZE];
	struct device_node *np;
};

#ifdef CONFIG_OF
static const struct of_device_id sec_therm_match[] = {
	{ .compatible = "samsung,sec-thermistor", },
	{ },
};
MODULE_DEVICE_TABLE(of, sec_therm_match);

static int sec_therm_parse_dt(struct platform_device *pdev)
{
	struct sec_therm_info *info = platform_get_drvdata(pdev);
	struct sec_therm_platform_data *pdata;
	const char *name;
	int adc_arr_len, temp_arr_len;
	int i;
	u32 adc, tp;

	if (!info || !pdev->dev.of_node)
		return -ENODEV;

	info->np = pdev->dev.of_node;

	if (of_property_read_u32(info->np, "id", &info->id)) {
		dev_err(info->dev, "failed to get thermistor ID\n");
		return -EINVAL;
	}

	if (!of_property_read_string(info->np, "thermistor_name", &name)) {
		strlcpy(info->name, name, sizeof(info->name));
	} else {
		dev_err(info->dev, "failed to get thermistor name\n");
		return -EINVAL;
	}

	pdata = devm_kzalloc(info->dev, sizeof(*pdata), GFP_KERNEL);
	if (!pdata)
		return -ENOMEM;

	if (!of_get_property(info->np, "adc_array", &adc_arr_len))
		return -ENOENT;
	if (!of_get_property(info->np, "temp_array", &temp_arr_len))
		return -ENOENT;

	if (adc_arr_len != temp_arr_len) {
		dev_err(info->dev, "%s: invalid array length(%u,%u)\n",
				__func__, adc_arr_len, temp_arr_len);
		return -EINVAL;
	}

	pdata->adc_arr_size = adc_arr_len / sizeof(u32);
	pdata->adc_table = devm_kzalloc(&pdev->dev,
			sizeof(*pdata->adc_table) * pdata->adc_arr_size,
			GFP_KERNEL);
	if (!pdata->adc_table)
		return -ENOMEM;

	for (i = 0; i < pdata->adc_arr_size; i++) {
		if (of_property_read_u32_index(info->np, "adc_array", i, &adc))
			return -EINVAL;
		if (of_property_read_u32_index(info->np, "temp_array", i, &tp))
			return -EINVAL;

		pdata->adc_table[i].adc = (int)adc;
		pdata->adc_table[i].temperature = (int)tp;
	}

	info->pdata = pdata;

	return 0;
}
#else
static int sec_therm_parse_dt(struct platform_device *pdev) { return -ENODEV; }
#endif

static int sec_therm_get_adc_data(struct sec_therm_info *info)
{
	int adc_data;
	int adc_max = 0, adc_min = 0, adc_total = 0;
	int i;

	for (i = 0; i < ADC_SAMPLING_CNT; i++) {
		int ret = iio_read_channel_raw(info->chan, &adc_data);

		if (ret < 0) {
			dev_err(info->dev, "%s : err(%d) returned, skip read\n",
				__func__, adc_data);
			return ret;
		}

		if (i != 0) {
			if (adc_data > adc_max)
				adc_max = adc_data;
			else if (adc_data < adc_min)
				adc_min = adc_data;
		} else {
			adc_max = adc_data;
			adc_min = adc_data;
		}
		adc_total += adc_data;
	}

	return (adc_total - adc_max - adc_min) / (ADC_SAMPLING_CNT - 2);
}

static int convert_adc_to_temper(struct sec_therm_info *info, unsigned int adc)
{
	int low = 0;
	int high = 0;
	int temp = 0;
	int temp2 = 0;

	if (!info->pdata->adc_table || !info->pdata->adc_arr_size) {
		/* using fake temp */
		return 300;
	}

	high = info->pdata->adc_arr_size - 1;

	if (info->pdata->adc_table[low].adc >= adc)
		return info->pdata->adc_table[low].temperature;
	else if (info->pdata->adc_table[high].adc <= adc)
		return info->pdata->adc_table[high].temperature;

	while (low <= high) {
		int mid = 0;

		mid = (low + high) / 2;
		if (info->pdata->adc_table[mid].adc > adc)
			high = mid - 1;
		else if (info->pdata->adc_table[mid].adc < adc)
			low = mid + 1;
		else
			return info->pdata->adc_table[mid].temperature;
	}

	temp = info->pdata->adc_table[high].temperature;

	temp2 = (info->pdata->adc_table[low].temperature -
			info->pdata->adc_table[high].temperature) *
			(adc - info->pdata->adc_table[high].adc);

	temp += temp2 /
		(info->pdata->adc_table[low].adc -
			info->pdata->adc_table[high].adc);

	return temp;
}

static ssize_t sec_therm_show_temperature(struct device *dev,
				   struct device_attribute *attr,
				   char *buf)
{
	struct sec_therm_info *info = dev_get_drvdata(dev);
	int adc, temp;

	adc = sec_therm_get_adc_data(info);

	if (adc >= 0)
		temp = convert_adc_to_temper(info, adc);
	else
		return adc;

	return sprintf(buf, "%d\n", temp);
}

static ssize_t sec_therm_show_temp_adc(struct device *dev,
				   struct device_attribute *attr,
				   char *buf)
{
	struct sec_therm_info *info = dev_get_drvdata(dev);
	int adc;

	adc = sec_therm_get_adc_data(info);

	return sprintf(buf, "%d\n", adc);
}

static ssize_t sec_therm_show_name(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct sec_therm_info *info = dev_get_drvdata(dev);

	return sprintf(buf, "%s\n", info->name);
}

static SENSOR_DEVICE_ATTR(temperature, 0444, sec_therm_show_temperature,
		NULL, 0);
static SENSOR_DEVICE_ATTR(temp_adc, 0444, sec_therm_show_temp_adc, NULL, 0);
static DEVICE_ATTR(name, 0444, sec_therm_show_name, NULL);

static struct attribute *sec_therm_attributes[] = {
	&sensor_dev_attr_temperature.dev_attr.attr,
	&sensor_dev_attr_temp_adc.dev_attr.attr,
	&dev_attr_name.attr,
	NULL
};

static const struct attribute_group sec_therm_attr_group = {
	.attrs = sec_therm_attributes,
};

static struct sec_therm_info *g_ap_therm_info;
int sec_therm_get_ap_temperature(void)
{
	int adc;
	int temp;

	if (unlikely(!g_ap_therm_info))
		return -ENODEV;

	adc = sec_therm_get_adc_data(g_ap_therm_info);

	if (adc >= 0)
		temp = convert_adc_to_temper(g_ap_therm_info, adc);
	else
		return adc;

	return temp;
}

static int sec_therm_probe(struct platform_device *pdev)
{
	struct sec_therm_info *info;
	int ret;

	dev_dbg(&pdev->dev, "%s: SEC Thermistor Driver Loading\n", __func__);

	info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
	if (!info)
		return -ENOMEM;

	platform_set_drvdata(pdev, info);
	info->dev = &pdev->dev;

	ret = sec_therm_parse_dt(pdev);
	if (ret) {
		dev_err(info->dev, "%s: fail to parse dt\n", __func__);
		return ret;
	}

	info->chan = iio_channel_get(info->dev, NULL);
	if (IS_ERR(info->chan)) {
		dev_err(info->dev, "%s: fail to get iio channel\n", __func__);
		return PTR_ERR(info->chan);
	}

	info->sec_dev = sec_device_create(info, info->name);
	if (IS_ERR(info->sec_dev)) {
		dev_err(info->dev, "%s: fail to create sec_dev\n", __func__);
		return PTR_ERR(info->sec_dev);
	}

	ret = sysfs_create_group(&info->sec_dev->kobj, &sec_therm_attr_group);
	if (ret) {
		dev_err(info->dev, "failed to create sysfs group\n");
		goto err_create_sysfs;
	}

	info->hwmon_dev = hwmon_device_register(info->dev);
	if (IS_ERR(info->hwmon_dev)) {
		dev_err(info->dev, "unable to register as hwmon device.\n");
		ret = PTR_ERR(info->hwmon_dev);
		goto err_register_hwmon;
	}

	if (info->id == 0)
		g_ap_therm_info = info;

	dev_info(info->dev, "%s successfully probed.\n", info->name);

	return 0;

err_register_hwmon:
	sysfs_remove_group(&info->sec_dev->kobj, &sec_therm_attr_group);
err_create_sysfs:
	sec_device_destroy(info->sec_dev->devt);
	return ret;
}

static int sec_therm_remove(struct platform_device *pdev)
{
	struct sec_therm_info *info = platform_get_drvdata(pdev);

	if (!info)
		return 0;

	if (info->id == 0)
		g_ap_therm_info = NULL;

	hwmon_device_unregister(info->hwmon_dev);
	sysfs_remove_group(&info->sec_dev->kobj, &sec_therm_attr_group);
	iio_channel_release(info->chan);
	sec_device_destroy(info->sec_dev->devt);
	platform_set_drvdata(pdev, NULL);

	return 0;
}

static struct platform_driver sec_thermistor_driver = {
	.driver = {
		.name = "sec-thermistor",
		.owner = THIS_MODULE,
		.of_match_table = of_match_ptr(sec_therm_match),
	},
	.probe = sec_therm_probe,
	.remove = sec_therm_remove,
};

module_platform_driver(sec_thermistor_driver);

MODULE_DESCRIPTION("SEC Thermistor Driver");
MODULE_AUTHOR("Minsung Kim <ms925.kim@samsung.com>");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:sec-thermistor");