Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

iio: ti_am335x_adc: Allow to specify input line

The TSC part allows to specify the input lines. The IIO part assumes
that it usues always the last few, that means if IIO has adc-channels
set to 2 it will use channel 6 and 7. However it might make sense to use
only 6.
This patch changes the device property (which was introduced recently
and was never in an official release) in a way that the user can specify
which of the AIN lines should be used. In Addition to this, the name is
now AINx where x is the channel number i.e. for AIN6 we would have 6.
Prior this, it always started counting at 0 which is confusing. In
addition to this, it also checks for correct step number during reading
and does not rely on proper FIFO depth.

Acked-by: Jonathan Cameron <jic23@kernel.org>
Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>

+56 -23
+1 -1
arch/arm/boot/dts/am335x-evm.dts
··· 255 255 }; 256 256 257 257 adc { 258 - ti,adc-channels = <4>; 258 + ti,adc-channels = <4 5 6 7>; 259 259 }; 260 260 };
+37 -20
drivers/iio/adc/ti_am335x_adc.c
··· 32 32 struct tiadc_device { 33 33 struct ti_tscadc_dev *mfd_tscadc; 34 34 int channels; 35 + u8 channel_line[8]; 36 + u8 channel_step[8]; 35 37 }; 36 38 37 39 static unsigned int tiadc_readl(struct tiadc_device *adc, unsigned int reg) ··· 59 57 static void tiadc_step_config(struct tiadc_device *adc_dev) 60 58 { 61 59 unsigned int stepconfig; 62 - int i, channels = 0, steps; 60 + int i, steps; 63 61 u32 step_en; 64 62 65 63 /* ··· 73 71 */ 74 72 75 73 steps = TOTAL_STEPS - adc_dev->channels; 76 - channels = TOTAL_CHANNELS - adc_dev->channels; 77 - 78 74 stepconfig = STEPCONFIG_AVG_16 | STEPCONFIG_FIFO1; 79 75 80 - for (i = steps; i < TOTAL_STEPS; i++) { 81 - tiadc_writel(adc_dev, REG_STEPCONFIG(i), 82 - stepconfig | STEPCONFIG_INP(channels)); 83 - tiadc_writel(adc_dev, REG_STEPDELAY(i), 76 + for (i = 0; i < adc_dev->channels; i++) { 77 + int chan; 78 + 79 + chan = adc_dev->channel_line[i]; 80 + tiadc_writel(adc_dev, REG_STEPCONFIG(steps), 81 + stepconfig | STEPCONFIG_INP(chan)); 82 + tiadc_writel(adc_dev, REG_STEPDELAY(steps), 84 83 STEPCONFIG_OPENDLY); 85 - channels++; 84 + adc_dev->channel_step[i] = steps; 85 + steps++; 86 86 } 87 87 step_en = get_adc_step_mask(adc_dev); 88 88 am335x_tsc_se_set(adc_dev->mfd_tscadc, step_en); ··· 119 115 120 116 chan->type = IIO_VOLTAGE; 121 117 chan->indexed = 1; 122 - chan->channel = i; 118 + chan->channel = adc_dev->channel_line[i]; 123 119 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW); 124 - chan->datasheet_name = chan_name_ain[i]; 120 + chan->datasheet_name = chan_name_ain[chan->channel]; 125 121 chan->scan_type.sign = 'u'; 126 122 chan->scan_type.realbits = 12; 127 123 chan->scan_type.storagebits = 32; ··· 143 139 { 144 140 struct tiadc_device *adc_dev = iio_priv(indio_dev); 145 141 int i; 146 - unsigned int fifo1count, readx1; 142 + unsigned int fifo1count, read; 143 + u32 step = UINT_MAX; 147 144 148 145 /* 149 146 * When the sub-system is first enabled, ··· 157 152 * Hence we need to flush out this data. 158 153 */ 159 154 155 + for (i = 0; i < ARRAY_SIZE(adc_dev->channel_step); i++) { 156 + if (chan->channel == adc_dev->channel_line[i]) { 157 + step = adc_dev->channel_step[i]; 158 + break; 159 + } 160 + } 161 + if (WARN_ON_ONCE(step == UINT_MAX)) 162 + return -EINVAL; 163 + 160 164 fifo1count = tiadc_readl(adc_dev, REG_FIFO1CNT); 161 165 for (i = 0; i < fifo1count; i++) { 162 - readx1 = tiadc_readl(adc_dev, REG_FIFO1); 163 - if (i == chan->channel) 164 - *val = readx1 & 0xfff; 166 + read = tiadc_readl(adc_dev, REG_FIFO1); 167 + if (read >> 16 == step) 168 + *val = read & 0xfff; 165 169 } 166 170 am335x_tsc_se_update(adc_dev->mfd_tscadc); 167 171 ··· 186 172 struct iio_dev *indio_dev; 187 173 struct tiadc_device *adc_dev; 188 174 struct device_node *node = pdev->dev.of_node; 175 + struct property *prop; 176 + const __be32 *cur; 189 177 int err; 190 - u32 val32; 178 + u32 val; 179 + int channels = 0; 191 180 192 181 if (!node) { 193 182 dev_err(&pdev->dev, "Could not find valid DT data.\n"); ··· 207 190 208 191 adc_dev->mfd_tscadc = ti_tscadc_dev_get(pdev); 209 192 210 - err = of_property_read_u32(node, 211 - "ti,adc-channels", &val32); 212 - if (err < 0) 213 - goto err_free_device; 214 - adc_dev->channels = val32; 193 + of_property_for_each_u32(node, "ti,adc-channels", prop, cur, val) { 194 + adc_dev->channel_line[channels] = val; 195 + channels++; 196 + } 197 + adc_dev->channels = channels; 215 198 216 199 indio_dev->dev.parent = &pdev->dev; 217 200 indio_dev->name = dev_name(&pdev->dev);
+18 -2
drivers/mfd/ti_am335x_tscadc.c
··· 91 91 struct clk *clk; 92 92 struct device_node *node = pdev->dev.of_node; 93 93 struct mfd_cell *cell; 94 + struct property *prop; 95 + const __be32 *cur; 96 + u32 val; 94 97 int err, ctrl; 95 98 int clk_value, clock_rate; 96 99 int tsc_wires = 0, adc_channels = 0, total_channels; 100 + int readouts = 0; 97 101 98 102 if (!pdev->dev.of_node) { 99 103 dev_err(&pdev->dev, "Could not find valid DT data.\n"); ··· 106 102 107 103 node = of_get_child_by_name(pdev->dev.of_node, "tsc"); 108 104 of_property_read_u32(node, "ti,wires", &tsc_wires); 105 + of_property_read_u32(node, "ti,coordiante-readouts", &readouts); 109 106 110 107 node = of_get_child_by_name(pdev->dev.of_node, "adc"); 111 - of_property_read_u32(node, "ti,adc-channels", &adc_channels); 112 - 108 + of_property_for_each_u32(node, "ti,adc-channels", prop, cur, val) { 109 + adc_channels++; 110 + if (val > 7) { 111 + dev_err(&pdev->dev, " PIN numbers are 0..7 (not %d)\n", 112 + val); 113 + return -EINVAL; 114 + } 115 + } 113 116 total_channels = tsc_wires + adc_channels; 114 117 if (total_channels > 8) { 115 118 dev_err(&pdev->dev, "Number of i/p channels more than 8\n"); ··· 124 113 } 125 114 if (total_channels == 0) { 126 115 dev_err(&pdev->dev, "Need atleast one channel.\n"); 116 + return -EINVAL; 117 + } 118 + 119 + if (readouts * 2 + 2 + adc_channels > 16) { 120 + dev_err(&pdev->dev, "Too many step configurations requested\n"); 127 121 return -EINVAL; 128 122 } 129 123