Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 1 | /* |
| 2 | * ST SPEAr ADC driver |
| 3 | * |
| 4 | * Copyright 2012 Stefan Roese <sr@denx.de> |
| 5 | * |
| 6 | * Licensed under the GPL-2. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/platform_device.h> |
| 11 | #include <linux/interrupt.h> |
| 12 | #include <linux/device.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/io.h> |
| 16 | #include <linux/clk.h> |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/completion.h> |
| 19 | #include <linux/of.h> |
| 20 | #include <linux/of_address.h> |
| 21 | |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 22 | #include <linux/iio/iio.h> |
| 23 | #include <linux/iio/sysfs.h> |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 24 | |
Jonathan Cameron | b64aef7 | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 25 | /* SPEAR registers definitions */ |
| 26 | #define SPEAR600_ADC_SCAN_RATE_LO(x) ((x) & 0xFFFF) |
| 27 | #define SPEAR600_ADC_SCAN_RATE_HI(x) (((x) >> 0x10) & 0xFFFF) |
| 28 | #define SPEAR_ADC_CLK_LOW(x) (((x) & 0xf) << 0) |
| 29 | #define SPEAR_ADC_CLK_HIGH(x) (((x) & 0xf) << 4) |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 30 | |
| 31 | /* Bit definitions for SPEAR_ADC_STATUS */ |
Haneen Mohammed | 418880f | 2015-03-26 02:23:29 +0300 | [diff] [blame] | 32 | #define SPEAR_ADC_STATUS_START_CONVERSION BIT(0) |
Jonathan Cameron | b64aef7 | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 33 | #define SPEAR_ADC_STATUS_CHANNEL_NUM(x) ((x) << 1) |
Haneen Mohammed | 418880f | 2015-03-26 02:23:29 +0300 | [diff] [blame] | 34 | #define SPEAR_ADC_STATUS_ADC_ENABLE BIT(4) |
Jonathan Cameron | b64aef7 | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 35 | #define SPEAR_ADC_STATUS_AVG_SAMPLE(x) ((x) << 5) |
Haneen Mohammed | 418880f | 2015-03-26 02:23:29 +0300 | [diff] [blame] | 36 | #define SPEAR_ADC_STATUS_VREF_INTERNAL BIT(9) |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 37 | |
Jonathan Cameron | b64aef7 | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 38 | #define SPEAR_ADC_DATA_MASK 0x03ff |
| 39 | #define SPEAR_ADC_DATA_BITS 10 |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 40 | |
Jonathan Cameron | b64aef7 | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 41 | #define SPEAR_ADC_MOD_NAME "spear-adc" |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 42 | |
Jonathan Cameron | b64aef7 | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 43 | #define SPEAR_ADC_CHANNEL_NUM 8 |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 44 | |
Jonathan Cameron | b64aef7 | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 45 | #define SPEAR_ADC_CLK_MIN 2500000 |
| 46 | #define SPEAR_ADC_CLK_MAX 20000000 |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 47 | |
| 48 | struct adc_regs_spear3xx { |
| 49 | u32 status; |
| 50 | u32 average; |
| 51 | u32 scan_rate; |
| 52 | u32 clk; /* Not avail for 1340 & 1310 */ |
Jonathan Cameron | b64aef7 | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 53 | u32 ch_ctrl[SPEAR_ADC_CHANNEL_NUM]; |
| 54 | u32 ch_data[SPEAR_ADC_CHANNEL_NUM]; |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | struct chan_data { |
| 58 | u32 lsb; |
| 59 | u32 msb; |
| 60 | }; |
| 61 | |
| 62 | struct adc_regs_spear6xx { |
| 63 | u32 status; |
| 64 | u32 pad[2]; |
| 65 | u32 clk; |
Jonathan Cameron | b64aef7 | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 66 | u32 ch_ctrl[SPEAR_ADC_CHANNEL_NUM]; |
| 67 | struct chan_data ch_data[SPEAR_ADC_CHANNEL_NUM]; |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 68 | u32 scan_rate_lo; |
| 69 | u32 scan_rate_hi; |
| 70 | struct chan_data average; |
| 71 | }; |
| 72 | |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 73 | struct spear_adc_state { |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 74 | struct device_node *np; |
| 75 | struct adc_regs_spear3xx __iomem *adc_base_spear3xx; |
| 76 | struct adc_regs_spear6xx __iomem *adc_base_spear6xx; |
| 77 | struct clk *clk; |
| 78 | struct completion completion; |
| 79 | u32 current_clk; |
| 80 | u32 sampling_freq; |
| 81 | u32 avg_samples; |
| 82 | u32 vref_external; |
| 83 | u32 value; |
| 84 | }; |
| 85 | |
| 86 | /* |
| 87 | * Functions to access some SPEAr ADC register. Abstracted into |
| 88 | * static inline functions, because of different register offsets |
| 89 | * on different SoC variants (SPEAr300 vs SPEAr600 etc). |
| 90 | */ |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 91 | static void spear_adc_set_status(struct spear_adc_state *st, u32 val) |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 92 | { |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 93 | __raw_writel(val, &st->adc_base_spear6xx->status); |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 94 | } |
| 95 | |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 96 | static void spear_adc_set_clk(struct spear_adc_state *st, u32 val) |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 97 | { |
| 98 | u32 clk_high, clk_low, count; |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 99 | u32 apb_clk = clk_get_rate(st->clk); |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 100 | |
Tapasweni Pathak | ffceca8 | 2014-10-08 22:56:15 +0530 | [diff] [blame] | 101 | count = DIV_ROUND_UP(apb_clk, val); |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 102 | clk_low = count / 2; |
| 103 | clk_high = count - clk_low; |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 104 | st->current_clk = apb_clk / count; |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 105 | |
Jonathan Cameron | b64aef7 | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 106 | __raw_writel(SPEAR_ADC_CLK_LOW(clk_low) | SPEAR_ADC_CLK_HIGH(clk_high), |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 107 | &st->adc_base_spear6xx->clk); |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 108 | } |
| 109 | |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 110 | static void spear_adc_set_ctrl(struct spear_adc_state *st, int n, |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 111 | u32 val) |
| 112 | { |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 113 | __raw_writel(val, &st->adc_base_spear6xx->ch_ctrl[n]); |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 114 | } |
| 115 | |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 116 | static u32 spear_adc_get_average(struct spear_adc_state *st) |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 117 | { |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 118 | if (of_device_is_compatible(st->np, "st,spear600-adc")) { |
| 119 | return __raw_readl(&st->adc_base_spear6xx->average.msb) & |
Jonathan Cameron | b64aef7 | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 120 | SPEAR_ADC_DATA_MASK; |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 121 | } else { |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 122 | return __raw_readl(&st->adc_base_spear3xx->average) & |
Jonathan Cameron | b64aef7 | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 123 | SPEAR_ADC_DATA_MASK; |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 127 | static void spear_adc_set_scanrate(struct spear_adc_state *st, u32 rate) |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 128 | { |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 129 | if (of_device_is_compatible(st->np, "st,spear600-adc")) { |
Jonathan Cameron | b64aef7 | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 130 | __raw_writel(SPEAR600_ADC_SCAN_RATE_LO(rate), |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 131 | &st->adc_base_spear6xx->scan_rate_lo); |
Jonathan Cameron | b64aef7 | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 132 | __raw_writel(SPEAR600_ADC_SCAN_RATE_HI(rate), |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 133 | &st->adc_base_spear6xx->scan_rate_hi); |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 134 | } else { |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 135 | __raw_writel(rate, &st->adc_base_spear3xx->scan_rate); |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
Jonathan Cameron | e20d609 | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 139 | static int spear_adc_read_raw(struct iio_dev *indio_dev, |
| 140 | struct iio_chan_spec const *chan, |
| 141 | int *val, |
| 142 | int *val2, |
| 143 | long mask) |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 144 | { |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 145 | struct spear_adc_state *st = iio_priv(indio_dev); |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 146 | u32 status; |
| 147 | |
| 148 | switch (mask) { |
Jonathan Cameron | b11f98f | 2012-04-15 17:41:18 +0100 | [diff] [blame] | 149 | case IIO_CHAN_INFO_RAW: |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 150 | mutex_lock(&indio_dev->mlock); |
| 151 | |
Jonathan Cameron | b64aef7 | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 152 | status = SPEAR_ADC_STATUS_CHANNEL_NUM(chan->channel) | |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 153 | SPEAR_ADC_STATUS_AVG_SAMPLE(st->avg_samples) | |
Jonathan Cameron | b64aef7 | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 154 | SPEAR_ADC_STATUS_START_CONVERSION | |
| 155 | SPEAR_ADC_STATUS_ADC_ENABLE; |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 156 | if (st->vref_external == 0) |
Jonathan Cameron | b64aef7 | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 157 | status |= SPEAR_ADC_STATUS_VREF_INTERNAL; |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 158 | |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 159 | spear_adc_set_status(st, status); |
| 160 | wait_for_completion(&st->completion); /* set by ISR */ |
| 161 | *val = st->value; |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 162 | |
| 163 | mutex_unlock(&indio_dev->mlock); |
| 164 | |
| 165 | return IIO_VAL_INT; |
| 166 | |
| 167 | case IIO_CHAN_INFO_SCALE: |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 168 | *val = st->vref_external; |
Jonathan Cameron | b64aef7 | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 169 | *val2 = SPEAR_ADC_DATA_BITS; |
Lars-Peter Clausen | bfbab2b | 2013-09-28 10:31:00 +0100 | [diff] [blame] | 170 | return IIO_VAL_FRACTIONAL_LOG2; |
Jonathan Cameron | 932de74 | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 171 | case IIO_CHAN_INFO_SAMP_FREQ: |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 172 | *val = st->current_clk; |
Jonathan Cameron | 932de74 | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 173 | return IIO_VAL_INT; |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | return -EINVAL; |
| 177 | } |
| 178 | |
Jonathan Cameron | 932de74 | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 179 | static int spear_adc_write_raw(struct iio_dev *indio_dev, |
| 180 | struct iio_chan_spec const *chan, |
| 181 | int val, |
| 182 | int val2, |
| 183 | long mask) |
| 184 | { |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 185 | struct spear_adc_state *st = iio_priv(indio_dev); |
Jonathan Cameron | 932de74 | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 186 | int ret = 0; |
| 187 | |
| 188 | if (mask != IIO_CHAN_INFO_SAMP_FREQ) |
| 189 | return -EINVAL; |
| 190 | |
| 191 | mutex_lock(&indio_dev->mlock); |
| 192 | |
| 193 | if ((val < SPEAR_ADC_CLK_MIN) || |
Ioana Ciornei | e8ef49f | 2015-10-14 21:14:13 +0300 | [diff] [blame] | 194 | (val > SPEAR_ADC_CLK_MAX) || |
| 195 | (val2 != 0)) { |
Jonathan Cameron | 932de74 | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 196 | ret = -EINVAL; |
| 197 | goto out; |
| 198 | } |
| 199 | |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 200 | spear_adc_set_clk(st, val); |
Jonathan Cameron | 932de74 | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 201 | |
| 202 | out: |
| 203 | mutex_unlock(&indio_dev->mlock); |
| 204 | return ret; |
| 205 | } |
| 206 | |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 207 | #define SPEAR_ADC_CHAN(idx) { \ |
| 208 | .type = IIO_VOLTAGE, \ |
| 209 | .indexed = 1, \ |
Jonathan Cameron | f1e067b | 2013-03-04 21:06:04 +0000 | [diff] [blame] | 210 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
| 211 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ |
Jonathan Cameron | 932de74 | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 212 | .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),\ |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 213 | .channel = idx, \ |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 214 | } |
| 215 | |
Lars-Peter Clausen | f4e4b95 | 2012-08-09 08:51:00 +0100 | [diff] [blame] | 216 | static const struct iio_chan_spec spear_adc_iio_channels[] = { |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 217 | SPEAR_ADC_CHAN(0), |
| 218 | SPEAR_ADC_CHAN(1), |
| 219 | SPEAR_ADC_CHAN(2), |
| 220 | SPEAR_ADC_CHAN(3), |
| 221 | SPEAR_ADC_CHAN(4), |
| 222 | SPEAR_ADC_CHAN(5), |
| 223 | SPEAR_ADC_CHAN(6), |
| 224 | SPEAR_ADC_CHAN(7), |
| 225 | }; |
| 226 | |
| 227 | static irqreturn_t spear_adc_isr(int irq, void *dev_id) |
| 228 | { |
Tapasweni Pathak | 31f8f06 | 2014-10-30 17:02:25 +0530 | [diff] [blame] | 229 | struct spear_adc_state *st = dev_id; |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 230 | |
| 231 | /* Read value to clear IRQ */ |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 232 | st->value = spear_adc_get_average(st); |
| 233 | complete(&st->completion); |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 234 | |
| 235 | return IRQ_HANDLED; |
| 236 | } |
| 237 | |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 238 | static int spear_adc_configure(struct spear_adc_state *st) |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 239 | { |
| 240 | int i; |
| 241 | |
| 242 | /* Reset ADC core */ |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 243 | spear_adc_set_status(st, 0); |
| 244 | __raw_writel(0, &st->adc_base_spear6xx->clk); |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 245 | for (i = 0; i < 8; i++) |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 246 | spear_adc_set_ctrl(st, i, 0); |
| 247 | spear_adc_set_scanrate(st, 0); |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 248 | |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 249 | spear_adc_set_clk(st, st->sampling_freq); |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 250 | |
| 251 | return 0; |
| 252 | } |
| 253 | |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 254 | static const struct iio_info spear_adc_info = { |
Jonathan Cameron | e20d609 | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 255 | .read_raw = &spear_adc_read_raw, |
Jonathan Cameron | 932de74 | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 256 | .write_raw = &spear_adc_write_raw, |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 257 | .driver_module = THIS_MODULE, |
| 258 | }; |
| 259 | |
Bill Pemberton | 4ae1c61 | 2012-11-19 13:21:57 -0500 | [diff] [blame] | 260 | static int spear_adc_probe(struct platform_device *pdev) |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 261 | { |
| 262 | struct device_node *np = pdev->dev.of_node; |
| 263 | struct device *dev = &pdev->dev; |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 264 | struct spear_adc_state *st; |
Amitoj Kaur Chawla | 09b9da3 | 2016-02-29 13:03:46 +0530 | [diff] [blame] | 265 | struct resource *res; |
Jonathan Cameron | e90ba52 | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 266 | struct iio_dev *indio_dev = NULL; |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 267 | int ret = -ENODEV; |
| 268 | int irq; |
| 269 | |
Jonathan Cameron | e90ba52 | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 270 | indio_dev = devm_iio_device_alloc(dev, sizeof(struct spear_adc_state)); |
| 271 | if (!indio_dev) { |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 272 | dev_err(dev, "failed allocating iio device\n"); |
Sachin Kamat | 8c7f6d5 | 2013-07-22 12:02:00 +0100 | [diff] [blame] | 273 | return -ENOMEM; |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 274 | } |
| 275 | |
Jonathan Cameron | e90ba52 | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 276 | st = iio_priv(indio_dev); |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 277 | st->np = np; |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 278 | |
| 279 | /* |
| 280 | * SPEAr600 has a different register layout than other SPEAr SoC's |
| 281 | * (e.g. SPEAr3xx). Let's provide two register base addresses |
| 282 | * to support multi-arch kernels. |
| 283 | */ |
Amitoj Kaur Chawla | 09b9da3 | 2016-02-29 13:03:46 +0530 | [diff] [blame] | 284 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 285 | st->adc_base_spear6xx = devm_ioremap_resource(&pdev->dev, res); |
| 286 | if (IS_ERR(st->adc_base_spear6xx)) |
| 287 | return PTR_ERR(st->adc_base_spear6xx); |
| 288 | |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 289 | st->adc_base_spear3xx = |
| 290 | (struct adc_regs_spear3xx __iomem *)st->adc_base_spear6xx; |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 291 | |
Gujulan Elango, Hari Prasath (H.) | b8cc27c | 2016-02-02 12:56:32 +0000 | [diff] [blame] | 292 | st->clk = devm_clk_get(dev, NULL); |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 293 | if (IS_ERR(st->clk)) { |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 294 | dev_err(dev, "failed getting clock\n"); |
Amitoj Kaur Chawla | 09b9da3 | 2016-02-29 13:03:46 +0530 | [diff] [blame] | 295 | return PTR_ERR(st->clk); |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 296 | } |
| 297 | |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 298 | ret = clk_prepare_enable(st->clk); |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 299 | if (ret) { |
| 300 | dev_err(dev, "failed enabling clock\n"); |
Amitoj Kaur Chawla | 09b9da3 | 2016-02-29 13:03:46 +0530 | [diff] [blame] | 301 | return ret; |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | irq = platform_get_irq(pdev, 0); |
Lars-Peter Clausen | a47f6e0 | 2013-10-16 21:45:00 +0100 | [diff] [blame] | 305 | if (irq <= 0) { |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 306 | dev_err(dev, "failed getting interrupt resource\n"); |
| 307 | ret = -EINVAL; |
Gujulan Elango, Hari Prasath (H.) | b8cc27c | 2016-02-02 12:56:32 +0000 | [diff] [blame] | 308 | goto errout2; |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 309 | } |
| 310 | |
Jonathan Cameron | b64aef7 | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 311 | ret = devm_request_irq(dev, irq, spear_adc_isr, 0, SPEAR_ADC_MOD_NAME, |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 312 | st); |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 313 | if (ret < 0) { |
| 314 | dev_err(dev, "failed requesting interrupt\n"); |
Gujulan Elango, Hari Prasath (H.) | b8cc27c | 2016-02-02 12:56:32 +0000 | [diff] [blame] | 315 | goto errout2; |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | if (of_property_read_u32(np, "sampling-frequency", |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 319 | &st->sampling_freq)) { |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 320 | dev_err(dev, "sampling-frequency missing in DT\n"); |
| 321 | ret = -EINVAL; |
Gujulan Elango, Hari Prasath (H.) | b8cc27c | 2016-02-02 12:56:32 +0000 | [diff] [blame] | 322 | goto errout2; |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | /* |
| 326 | * Optional avg_samples defaults to 0, resulting in single data |
| 327 | * conversion |
| 328 | */ |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 329 | of_property_read_u32(np, "average-samples", &st->avg_samples); |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 330 | |
| 331 | /* |
| 332 | * Optional vref_external defaults to 0, resulting in internal vref |
| 333 | * selection |
| 334 | */ |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 335 | of_property_read_u32(np, "vref-external", &st->vref_external); |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 336 | |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 337 | spear_adc_configure(st); |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 338 | |
Jonathan Cameron | e90ba52 | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 339 | platform_set_drvdata(pdev, indio_dev); |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 340 | |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 341 | init_completion(&st->completion); |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 342 | |
Jonathan Cameron | e90ba52 | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 343 | indio_dev->name = SPEAR_ADC_MOD_NAME; |
| 344 | indio_dev->dev.parent = dev; |
| 345 | indio_dev->info = &spear_adc_info; |
| 346 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 347 | indio_dev->channels = spear_adc_iio_channels; |
| 348 | indio_dev->num_channels = ARRAY_SIZE(spear_adc_iio_channels); |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 349 | |
Jonathan Cameron | e90ba52 | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 350 | ret = iio_device_register(indio_dev); |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 351 | if (ret) |
Gujulan Elango, Hari Prasath (H.) | b8cc27c | 2016-02-02 12:56:32 +0000 | [diff] [blame] | 352 | goto errout2; |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 353 | |
| 354 | dev_info(dev, "SPEAR ADC driver loaded, IRQ %d\n", irq); |
| 355 | |
| 356 | return 0; |
| 357 | |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 358 | errout2: |
Gujulan Elango, Hari Prasath (H.) | b8cc27c | 2016-02-02 12:56:32 +0000 | [diff] [blame] | 359 | clk_disable_unprepare(st->clk); |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 360 | return ret; |
| 361 | } |
| 362 | |
Bill Pemberton | 447d4f2 | 2012-11-19 13:26:37 -0500 | [diff] [blame] | 363 | static int spear_adc_remove(struct platform_device *pdev) |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 364 | { |
Jonathan Cameron | e90ba52 | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 365 | struct iio_dev *indio_dev = platform_get_drvdata(pdev); |
| 366 | struct spear_adc_state *st = iio_priv(indio_dev); |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 367 | |
Jonathan Cameron | e90ba52 | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 368 | iio_device_unregister(indio_dev); |
Jonathan Cameron | b586e5d | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 369 | clk_disable_unprepare(st->clk); |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 370 | |
| 371 | return 0; |
| 372 | } |
| 373 | |
Sachin Kamat | e89b674 | 2013-06-07 12:06:00 +0100 | [diff] [blame] | 374 | #ifdef CONFIG_OF |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 375 | static const struct of_device_id spear_adc_dt_ids[] = { |
| 376 | { .compatible = "st,spear600-adc", }, |
| 377 | { /* sentinel */ } |
| 378 | }; |
| 379 | MODULE_DEVICE_TABLE(of, spear_adc_dt_ids); |
Sachin Kamat | e89b674 | 2013-06-07 12:06:00 +0100 | [diff] [blame] | 380 | #endif |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 381 | |
| 382 | static struct platform_driver spear_adc_driver = { |
| 383 | .probe = spear_adc_probe, |
Bill Pemberton | e543acf | 2012-11-19 13:21:38 -0500 | [diff] [blame] | 384 | .remove = spear_adc_remove, |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 385 | .driver = { |
Jonathan Cameron | b64aef7 | 2014-05-04 17:45:00 +0100 | [diff] [blame] | 386 | .name = SPEAR_ADC_MOD_NAME, |
Stefan Roese | b3201b5 | 2012-04-12 11:05:35 +0200 | [diff] [blame] | 387 | .of_match_table = of_match_ptr(spear_adc_dt_ids), |
| 388 | }, |
| 389 | }; |
| 390 | |
| 391 | module_platform_driver(spear_adc_driver); |
| 392 | |
| 393 | MODULE_AUTHOR("Stefan Roese <sr@denx.de>"); |
| 394 | MODULE_DESCRIPTION("SPEAr ADC driver"); |
| 395 | MODULE_LICENSE("GPL"); |