2 * Aspeed AST2400/2500 ADC
4 * Copyright (C) 2017 Google, Inc.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
12 #include <linux/clk.h>
13 #include <linux/clk-provider.h>
14 #include <linux/err.h>
15 #include <linux/errno.h>
17 #include <linux/module.h>
18 #include <linux/of_platform.h>
19 #include <linux/platform_device.h>
20 #include <linux/reset.h>
21 #include <linux/spinlock.h>
22 #include <linux/types.h>
24 #include <linux/iio/iio.h>
25 #include <linux/iio/driver.h>
26 #include <linux/iopoll.h>
28 #define ASPEED_RESOLUTION_BITS 10
29 #define ASPEED_CLOCKS_PER_SAMPLE 12
31 #define ASPEED_REG_ENGINE_CONTROL 0x00
32 #define ASPEED_REG_INTERRUPT_CONTROL 0x04
33 #define ASPEED_REG_VGA_DETECT_CONTROL 0x08
34 #define ASPEED_REG_CLOCK_CONTROL 0x0C
35 #define ASPEED_REG_MAX 0xC0
37 #define ASPEED_OPERATION_MODE_POWER_DOWN (0x0 << 1)
38 #define ASPEED_OPERATION_MODE_STANDBY (0x1 << 1)
39 #define ASPEED_OPERATION_MODE_NORMAL (0x7 << 1)
41 #define ASPEED_ENGINE_ENABLE BIT(0)
43 #define ASPEED_ADC_CTRL_INIT_RDY BIT(8)
45 #define ASPEED_ADC_INIT_POLLING_TIME 500
46 #define ASPEED_ADC_INIT_TIMEOUT 500000
48 struct aspeed_adc_model_data
{
49 const char *model_name
;
50 unsigned int min_sampling_rate
; // Hz
51 unsigned int max_sampling_rate
; // Hz
52 unsigned int vref_voltage
; // mV
53 bool wait_init_sequence
;
56 struct aspeed_adc_data
{
60 struct clk_hw
*clk_prescaler
;
61 struct clk_hw
*clk_scaler
;
62 struct reset_control
*rst
;
65 #define ASPEED_CHAN(_idx, _data_reg_addr) { \
66 .type = IIO_VOLTAGE, \
69 .address = (_data_reg_addr), \
70 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
71 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
72 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
75 static const struct iio_chan_spec aspeed_adc_iio_channels
[] = {
86 ASPEED_CHAN(10, 0x24),
87 ASPEED_CHAN(11, 0x26),
88 ASPEED_CHAN(12, 0x28),
89 ASPEED_CHAN(13, 0x2A),
90 ASPEED_CHAN(14, 0x2C),
91 ASPEED_CHAN(15, 0x2E),
94 static int aspeed_adc_read_raw(struct iio_dev
*indio_dev
,
95 struct iio_chan_spec
const *chan
,
96 int *val
, int *val2
, long mask
)
98 struct aspeed_adc_data
*data
= iio_priv(indio_dev
);
99 const struct aspeed_adc_model_data
*model_data
=
100 of_device_get_match_data(data
->dev
);
103 case IIO_CHAN_INFO_RAW
:
104 *val
= readw(data
->base
+ chan
->address
);
107 case IIO_CHAN_INFO_SCALE
:
108 *val
= model_data
->vref_voltage
;
109 *val2
= ASPEED_RESOLUTION_BITS
;
110 return IIO_VAL_FRACTIONAL_LOG2
;
112 case IIO_CHAN_INFO_SAMP_FREQ
:
113 *val
= clk_get_rate(data
->clk_scaler
->clk
) /
114 ASPEED_CLOCKS_PER_SAMPLE
;
122 static int aspeed_adc_write_raw(struct iio_dev
*indio_dev
,
123 struct iio_chan_spec
const *chan
,
124 int val
, int val2
, long mask
)
126 struct aspeed_adc_data
*data
= iio_priv(indio_dev
);
127 const struct aspeed_adc_model_data
*model_data
=
128 of_device_get_match_data(data
->dev
);
131 case IIO_CHAN_INFO_SAMP_FREQ
:
132 if (val
< model_data
->min_sampling_rate
||
133 val
> model_data
->max_sampling_rate
)
136 clk_set_rate(data
->clk_scaler
->clk
,
137 val
* ASPEED_CLOCKS_PER_SAMPLE
);
140 case IIO_CHAN_INFO_SCALE
:
141 case IIO_CHAN_INFO_RAW
:
143 * Technically, these could be written but the only reasons
144 * for doing so seem better handled in userspace. EPERM is
145 * returned to signal this is a policy choice rather than a
146 * hardware limitation.
155 static int aspeed_adc_reg_access(struct iio_dev
*indio_dev
,
156 unsigned int reg
, unsigned int writeval
,
157 unsigned int *readval
)
159 struct aspeed_adc_data
*data
= iio_priv(indio_dev
);
161 if (!readval
|| reg
% 4 || reg
> ASPEED_REG_MAX
)
164 *readval
= readl(data
->base
+ reg
);
169 static const struct iio_info aspeed_adc_iio_info
= {
170 .read_raw
= aspeed_adc_read_raw
,
171 .write_raw
= aspeed_adc_write_raw
,
172 .debugfs_reg_access
= aspeed_adc_reg_access
,
175 static int aspeed_adc_probe(struct platform_device
*pdev
)
177 struct iio_dev
*indio_dev
;
178 struct aspeed_adc_data
*data
;
179 const struct aspeed_adc_model_data
*model_data
;
180 struct resource
*res
;
181 const char *clk_parent_name
;
183 u32 adc_engine_control_reg_val
;
185 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(*data
));
189 data
= iio_priv(indio_dev
);
190 data
->dev
= &pdev
->dev
;
192 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
193 data
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
194 if (IS_ERR(data
->base
))
195 return PTR_ERR(data
->base
);
197 /* Register ADC clock prescaler with source specified by device tree. */
198 spin_lock_init(&data
->clk_lock
);
199 clk_parent_name
= of_clk_get_parent_name(pdev
->dev
.of_node
, 0);
201 data
->clk_prescaler
= clk_hw_register_divider(
202 &pdev
->dev
, "prescaler", clk_parent_name
, 0,
203 data
->base
+ ASPEED_REG_CLOCK_CONTROL
,
204 17, 15, 0, &data
->clk_lock
);
205 if (IS_ERR(data
->clk_prescaler
))
206 return PTR_ERR(data
->clk_prescaler
);
209 * Register ADC clock scaler downstream from the prescaler. Allow rate
210 * setting to adjust the prescaler as well.
212 data
->clk_scaler
= clk_hw_register_divider(
213 &pdev
->dev
, "scaler", "prescaler",
215 data
->base
+ ASPEED_REG_CLOCK_CONTROL
,
216 0, 10, 0, &data
->clk_lock
);
217 if (IS_ERR(data
->clk_scaler
)) {
218 ret
= PTR_ERR(data
->clk_scaler
);
222 data
->rst
= devm_reset_control_get_exclusive(&pdev
->dev
, NULL
);
223 if (IS_ERR(data
->rst
)) {
225 "invalid or missing reset controller device tree entry");
226 ret
= PTR_ERR(data
->rst
);
229 reset_control_deassert(data
->rst
);
231 model_data
= of_device_get_match_data(&pdev
->dev
);
233 if (model_data
->wait_init_sequence
) {
234 /* Enable engine in normal mode. */
235 writel(ASPEED_OPERATION_MODE_NORMAL
| ASPEED_ENGINE_ENABLE
,
236 data
->base
+ ASPEED_REG_ENGINE_CONTROL
);
238 /* Wait for initial sequence complete. */
239 ret
= readl_poll_timeout(data
->base
+ ASPEED_REG_ENGINE_CONTROL
,
240 adc_engine_control_reg_val
,
241 adc_engine_control_reg_val
&
242 ASPEED_ADC_CTRL_INIT_RDY
,
243 ASPEED_ADC_INIT_POLLING_TIME
,
244 ASPEED_ADC_INIT_TIMEOUT
);
246 goto poll_timeout_error
;
249 /* Start all channels in normal mode. */
250 ret
= clk_prepare_enable(data
->clk_scaler
->clk
);
252 goto clk_enable_error
;
254 adc_engine_control_reg_val
= GENMASK(31, 16) |
255 ASPEED_OPERATION_MODE_NORMAL
| ASPEED_ENGINE_ENABLE
;
256 writel(adc_engine_control_reg_val
,
257 data
->base
+ ASPEED_REG_ENGINE_CONTROL
);
259 model_data
= of_device_get_match_data(&pdev
->dev
);
260 indio_dev
->name
= model_data
->model_name
;
261 indio_dev
->dev
.parent
= &pdev
->dev
;
262 indio_dev
->info
= &aspeed_adc_iio_info
;
263 indio_dev
->modes
= INDIO_DIRECT_MODE
;
264 indio_dev
->channels
= aspeed_adc_iio_channels
;
265 indio_dev
->num_channels
= ARRAY_SIZE(aspeed_adc_iio_channels
);
267 ret
= iio_device_register(indio_dev
);
269 goto iio_register_error
;
274 writel(ASPEED_OPERATION_MODE_POWER_DOWN
,
275 data
->base
+ ASPEED_REG_ENGINE_CONTROL
);
276 clk_disable_unprepare(data
->clk_scaler
->clk
);
279 reset_control_assert(data
->rst
);
281 clk_hw_unregister_divider(data
->clk_scaler
);
283 clk_hw_unregister_divider(data
->clk_prescaler
);
287 static int aspeed_adc_remove(struct platform_device
*pdev
)
289 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
290 struct aspeed_adc_data
*data
= iio_priv(indio_dev
);
292 iio_device_unregister(indio_dev
);
293 writel(ASPEED_OPERATION_MODE_POWER_DOWN
,
294 data
->base
+ ASPEED_REG_ENGINE_CONTROL
);
295 clk_disable_unprepare(data
->clk_scaler
->clk
);
296 reset_control_assert(data
->rst
);
297 clk_hw_unregister_divider(data
->clk_scaler
);
298 clk_hw_unregister_divider(data
->clk_prescaler
);
303 static const struct aspeed_adc_model_data ast2400_model_data
= {
304 .model_name
= "ast2400-adc",
305 .vref_voltage
= 2500, // mV
306 .min_sampling_rate
= 10000,
307 .max_sampling_rate
= 500000,
310 static const struct aspeed_adc_model_data ast2500_model_data
= {
311 .model_name
= "ast2500-adc",
312 .vref_voltage
= 1800, // mV
313 .min_sampling_rate
= 1,
314 .max_sampling_rate
= 1000000,
315 .wait_init_sequence
= true,
318 static const struct of_device_id aspeed_adc_matches
[] = {
319 { .compatible
= "aspeed,ast2400-adc", .data
= &ast2400_model_data
},
320 { .compatible
= "aspeed,ast2500-adc", .data
= &ast2500_model_data
},
323 MODULE_DEVICE_TABLE(of
, aspeed_adc_matches
);
325 static struct platform_driver aspeed_adc_driver
= {
326 .probe
= aspeed_adc_probe
,
327 .remove
= aspeed_adc_remove
,
329 .name
= KBUILD_MODNAME
,
330 .of_match_table
= aspeed_adc_matches
,
334 module_platform_driver(aspeed_adc_driver
);
336 MODULE_AUTHOR("Rick Altherr <raltherr@google.com>");
337 MODULE_DESCRIPTION("Aspeed AST2400/2500 ADC Driver");
338 MODULE_LICENSE("GPL");