1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2016 MediaTek Inc.
4 * Author: Zhiyong Tao <zhiyong.tao@mediatek.com>
8 #include <linux/delay.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/mod_devicetable.h>
13 #include <linux/platform_device.h>
14 #include <linux/property.h>
15 #include <linux/iopoll.h>
17 #include <linux/iio/iio.h>
19 /* Register definitions */
20 #define MT6577_AUXADC_CON0 0x00
21 #define MT6577_AUXADC_CON1 0x04
22 #define MT6577_AUXADC_CON2 0x10
23 #define MT6577_AUXADC_STA BIT(0)
25 #define MT6577_AUXADC_DAT0 0x14
26 #define MT6577_AUXADC_RDY0 BIT(12)
28 #define MT6577_AUXADC_MISC 0x94
29 #define MT6577_AUXADC_PDN_EN BIT(14)
31 #define MT6577_AUXADC_DAT_MASK 0xfff
32 #define MT6577_AUXADC_SLEEP_US 1000
33 #define MT6577_AUXADC_TIMEOUT_US 10000
34 #define MT6577_AUXADC_POWER_READY_MS 1
35 #define MT6577_AUXADC_SAMPLE_READY_US 25
37 struct mtk_auxadc_compatible
{
38 bool sample_data_cali
;
39 bool check_global_idle
;
42 struct mt6577_auxadc_device
{
43 void __iomem
*reg_base
;
46 const struct mtk_auxadc_compatible
*dev_comp
;
49 static const struct mtk_auxadc_compatible mt8173_compat
= {
50 .sample_data_cali
= false,
51 .check_global_idle
= true,
54 static const struct mtk_auxadc_compatible mt6765_compat
= {
55 .sample_data_cali
= true,
56 .check_global_idle
= false,
59 #define MT6577_AUXADC_CHANNEL(idx) { \
60 .type = IIO_VOLTAGE, \
63 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
66 static const struct iio_chan_spec mt6577_auxadc_iio_channels
[] = {
67 MT6577_AUXADC_CHANNEL(0),
68 MT6577_AUXADC_CHANNEL(1),
69 MT6577_AUXADC_CHANNEL(2),
70 MT6577_AUXADC_CHANNEL(3),
71 MT6577_AUXADC_CHANNEL(4),
72 MT6577_AUXADC_CHANNEL(5),
73 MT6577_AUXADC_CHANNEL(6),
74 MT6577_AUXADC_CHANNEL(7),
75 MT6577_AUXADC_CHANNEL(8),
76 MT6577_AUXADC_CHANNEL(9),
77 MT6577_AUXADC_CHANNEL(10),
78 MT6577_AUXADC_CHANNEL(11),
79 MT6577_AUXADC_CHANNEL(12),
80 MT6577_AUXADC_CHANNEL(13),
81 MT6577_AUXADC_CHANNEL(14),
82 MT6577_AUXADC_CHANNEL(15),
85 static int mt_auxadc_get_cali_data(int rawdata
, bool enable_cali
)
90 static inline void mt6577_auxadc_mod_reg(void __iomem
*reg
,
91 u32 or_mask
, u32 and_mask
)
101 static int mt6577_auxadc_read(struct iio_dev
*indio_dev
,
102 struct iio_chan_spec
const *chan
)
105 void __iomem
*reg_channel
;
107 struct mt6577_auxadc_device
*adc_dev
= iio_priv(indio_dev
);
109 reg_channel
= adc_dev
->reg_base
+ MT6577_AUXADC_DAT0
+
110 chan
->channel
* 0x04;
112 mutex_lock(&adc_dev
->lock
);
114 mt6577_auxadc_mod_reg(adc_dev
->reg_base
+ MT6577_AUXADC_CON1
,
115 0, 1 << chan
->channel
);
117 /* read channel and make sure old ready bit == 0 */
118 ret
= readl_poll_timeout(reg_channel
, val
,
119 ((val
& MT6577_AUXADC_RDY0
) == 0),
120 MT6577_AUXADC_SLEEP_US
,
121 MT6577_AUXADC_TIMEOUT_US
);
123 dev_err(indio_dev
->dev
.parent
,
124 "wait for channel[%d] ready bit clear time out\n",
129 /* set bit to trigger sample */
130 mt6577_auxadc_mod_reg(adc_dev
->reg_base
+ MT6577_AUXADC_CON1
,
131 1 << chan
->channel
, 0);
133 /* we must delay here for hardware sample channel data */
134 udelay(MT6577_AUXADC_SAMPLE_READY_US
);
136 if (adc_dev
->dev_comp
->check_global_idle
) {
137 /* check MTK_AUXADC_CON2 if auxadc is idle */
138 ret
= readl_poll_timeout(adc_dev
->reg_base
+ MT6577_AUXADC_CON2
,
139 val
, ((val
& MT6577_AUXADC_STA
) == 0),
140 MT6577_AUXADC_SLEEP_US
,
141 MT6577_AUXADC_TIMEOUT_US
);
143 dev_err(indio_dev
->dev
.parent
,
144 "wait for auxadc idle time out\n");
149 /* read channel and make sure ready bit == 1 */
150 ret
= readl_poll_timeout(reg_channel
, val
,
151 ((val
& MT6577_AUXADC_RDY0
) != 0),
152 MT6577_AUXADC_SLEEP_US
,
153 MT6577_AUXADC_TIMEOUT_US
);
155 dev_err(indio_dev
->dev
.parent
,
156 "wait for channel[%d] data ready time out\n",
162 val
= readl(reg_channel
) & MT6577_AUXADC_DAT_MASK
;
164 mutex_unlock(&adc_dev
->lock
);
170 mutex_unlock(&adc_dev
->lock
);
175 static int mt6577_auxadc_read_raw(struct iio_dev
*indio_dev
,
176 struct iio_chan_spec
const *chan
,
181 struct mt6577_auxadc_device
*adc_dev
= iio_priv(indio_dev
);
184 case IIO_CHAN_INFO_PROCESSED
:
185 *val
= mt6577_auxadc_read(indio_dev
, chan
);
187 dev_err(indio_dev
->dev
.parent
,
188 "failed to sample data on channel[%d]\n",
192 if (adc_dev
->dev_comp
->sample_data_cali
)
193 *val
= mt_auxadc_get_cali_data(*val
, true);
201 static const struct iio_info mt6577_auxadc_info
= {
202 .read_raw
= &mt6577_auxadc_read_raw
,
205 static int __maybe_unused
mt6577_auxadc_resume(struct device
*dev
)
207 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
208 struct mt6577_auxadc_device
*adc_dev
= iio_priv(indio_dev
);
211 ret
= clk_prepare_enable(adc_dev
->adc_clk
);
213 pr_err("failed to enable auxadc clock\n");
217 mt6577_auxadc_mod_reg(adc_dev
->reg_base
+ MT6577_AUXADC_MISC
,
218 MT6577_AUXADC_PDN_EN
, 0);
219 mdelay(MT6577_AUXADC_POWER_READY_MS
);
224 static int __maybe_unused
mt6577_auxadc_suspend(struct device
*dev
)
226 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
227 struct mt6577_auxadc_device
*adc_dev
= iio_priv(indio_dev
);
229 mt6577_auxadc_mod_reg(adc_dev
->reg_base
+ MT6577_AUXADC_MISC
,
230 0, MT6577_AUXADC_PDN_EN
);
231 clk_disable_unprepare(adc_dev
->adc_clk
);
236 static int mt6577_auxadc_probe(struct platform_device
*pdev
)
238 struct mt6577_auxadc_device
*adc_dev
;
239 unsigned long adc_clk_rate
;
240 struct iio_dev
*indio_dev
;
243 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(*adc_dev
));
247 adc_dev
= iio_priv(indio_dev
);
248 indio_dev
->name
= dev_name(&pdev
->dev
);
249 indio_dev
->info
= &mt6577_auxadc_info
;
250 indio_dev
->modes
= INDIO_DIRECT_MODE
;
251 indio_dev
->channels
= mt6577_auxadc_iio_channels
;
252 indio_dev
->num_channels
= ARRAY_SIZE(mt6577_auxadc_iio_channels
);
254 adc_dev
->reg_base
= devm_platform_ioremap_resource(pdev
, 0);
255 if (IS_ERR(adc_dev
->reg_base
)) {
256 dev_err(&pdev
->dev
, "failed to get auxadc base address\n");
257 return PTR_ERR(adc_dev
->reg_base
);
260 adc_dev
->adc_clk
= devm_clk_get(&pdev
->dev
, "main");
261 if (IS_ERR(adc_dev
->adc_clk
)) {
262 dev_err(&pdev
->dev
, "failed to get auxadc clock\n");
263 return PTR_ERR(adc_dev
->adc_clk
);
266 ret
= clk_prepare_enable(adc_dev
->adc_clk
);
268 dev_err(&pdev
->dev
, "failed to enable auxadc clock\n");
272 adc_clk_rate
= clk_get_rate(adc_dev
->adc_clk
);
275 dev_err(&pdev
->dev
, "null clock rate\n");
276 goto err_disable_clk
;
279 adc_dev
->dev_comp
= device_get_match_data(&pdev
->dev
);
281 mutex_init(&adc_dev
->lock
);
283 mt6577_auxadc_mod_reg(adc_dev
->reg_base
+ MT6577_AUXADC_MISC
,
284 MT6577_AUXADC_PDN_EN
, 0);
285 mdelay(MT6577_AUXADC_POWER_READY_MS
);
287 platform_set_drvdata(pdev
, indio_dev
);
289 ret
= iio_device_register(indio_dev
);
291 dev_err(&pdev
->dev
, "failed to register iio device\n");
298 mt6577_auxadc_mod_reg(adc_dev
->reg_base
+ MT6577_AUXADC_MISC
,
299 0, MT6577_AUXADC_PDN_EN
);
301 clk_disable_unprepare(adc_dev
->adc_clk
);
305 static int mt6577_auxadc_remove(struct platform_device
*pdev
)
307 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
308 struct mt6577_auxadc_device
*adc_dev
= iio_priv(indio_dev
);
310 iio_device_unregister(indio_dev
);
312 mt6577_auxadc_mod_reg(adc_dev
->reg_base
+ MT6577_AUXADC_MISC
,
313 0, MT6577_AUXADC_PDN_EN
);
315 clk_disable_unprepare(adc_dev
->adc_clk
);
320 static SIMPLE_DEV_PM_OPS(mt6577_auxadc_pm_ops
,
321 mt6577_auxadc_suspend
,
322 mt6577_auxadc_resume
);
324 static const struct of_device_id mt6577_auxadc_of_match
[] = {
325 { .compatible
= "mediatek,mt2701-auxadc", .data
= &mt8173_compat
},
326 { .compatible
= "mediatek,mt2712-auxadc", .data
= &mt8173_compat
},
327 { .compatible
= "mediatek,mt7622-auxadc", .data
= &mt8173_compat
},
328 { .compatible
= "mediatek,mt8173-auxadc", .data
= &mt8173_compat
},
329 { .compatible
= "mediatek,mt6765-auxadc", .data
= &mt6765_compat
},
332 MODULE_DEVICE_TABLE(of
, mt6577_auxadc_of_match
);
334 static struct platform_driver mt6577_auxadc_driver
= {
336 .name
= "mt6577-auxadc",
337 .of_match_table
= mt6577_auxadc_of_match
,
338 .pm
= &mt6577_auxadc_pm_ops
,
340 .probe
= mt6577_auxadc_probe
,
341 .remove
= mt6577_auxadc_remove
,
343 module_platform_driver(mt6577_auxadc_driver
);
345 MODULE_AUTHOR("Zhiyong Tao <zhiyong.tao@mediatek.com>");
346 MODULE_DESCRIPTION("MTK AUXADC Device Driver");
347 MODULE_LICENSE("GPL v2");