1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Azoteq IQS620AT Temperature Sensor
4  *
5  * Copyright (C) 2019 Jeff LaBundy <jeff@labundy.com>
6  */
7 
8 #include <linux/device.h>
9 #include <linux/iio/iio.h>
10 #include <linux/kernel.h>
11 #include <linux/mfd/iqs62x.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/regmap.h>
15 
16 #define IQS620_TEMP_UI_OUT			0x1A
17 
18 #define IQS620_TEMP_SCALE			1000
19 #define IQS620_TEMP_OFFSET			(-100)
20 #define IQS620_TEMP_OFFSET_V3			(-40)
21 
22 static int iqs620_temp_read_raw(struct iio_dev *indio_dev,
23 				struct iio_chan_spec const *chan,
24 				int *val, int *val2, long mask)
25 {
26 	struct iqs62x_core *iqs62x = iio_device_get_drvdata(indio_dev);
27 	int ret;
28 	__le16 val_buf;
29 
30 	switch (mask) {
31 	case IIO_CHAN_INFO_RAW:
32 		ret = regmap_raw_read(iqs62x->regmap, IQS620_TEMP_UI_OUT,
33 				      &val_buf, sizeof(val_buf));
34 		if (ret)
35 			return ret;
36 
37 		*val = le16_to_cpu(val_buf);
38 		return IIO_VAL_INT;
39 
40 	case IIO_CHAN_INFO_SCALE:
41 		*val = IQS620_TEMP_SCALE;
42 		return IIO_VAL_INT;
43 
44 	case IIO_CHAN_INFO_OFFSET:
45 		*val = iqs62x->hw_num < IQS620_HW_NUM_V3 ? IQS620_TEMP_OFFSET
46 							 : IQS620_TEMP_OFFSET_V3;
47 		return IIO_VAL_INT;
48 
49 	default:
50 		return -EINVAL;
51 	}
52 }
53 
54 static const struct iio_info iqs620_temp_info = {
55 	.read_raw = &iqs620_temp_read_raw,
56 };
57 
58 static const struct iio_chan_spec iqs620_temp_channels[] = {
59 	{
60 		.type = IIO_TEMP,
61 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
62 				      BIT(IIO_CHAN_INFO_SCALE) |
63 				      BIT(IIO_CHAN_INFO_OFFSET),
64 	},
65 };
66 
67 static int iqs620_temp_probe(struct platform_device *pdev)
68 {
69 	struct iqs62x_core *iqs62x = dev_get_drvdata(pdev->dev.parent);
70 	struct iio_dev *indio_dev;
71 
72 	indio_dev = devm_iio_device_alloc(&pdev->dev, 0);
73 	if (!indio_dev)
74 		return -ENOMEM;
75 
76 	iio_device_set_drvdata(indio_dev, iqs62x);
77 
78 	indio_dev->modes = INDIO_DIRECT_MODE;
79 	indio_dev->channels = iqs620_temp_channels;
80 	indio_dev->num_channels = ARRAY_SIZE(iqs620_temp_channels);
81 	indio_dev->name = iqs62x->dev_desc->dev_name;
82 	indio_dev->info = &iqs620_temp_info;
83 
84 	return devm_iio_device_register(&pdev->dev, indio_dev);
85 }
86 
87 static struct platform_driver iqs620_temp_platform_driver = {
88 	.driver = {
89 		.name = "iqs620at-temp",
90 	},
91 	.probe = iqs620_temp_probe,
92 };
93 module_platform_driver(iqs620_temp_platform_driver);
94 
95 MODULE_AUTHOR("Jeff LaBundy <jeff@labundy.com>");
96 MODULE_DESCRIPTION("Azoteq IQS620AT Temperature Sensor");
97 MODULE_LICENSE("GPL");
98 MODULE_ALIAS("platform:iqs620at-temp");
99