From: Oleksandr Kozaruk <oleksandr.kozaruk-hExfYMNmJl/Cnp4W7fqMDg@public.gmane.org>
To: Lars-Peter Clausen <lars-Qo5EllUWu/uELgA04lAiVw@public.gmane.org>
Cc: tony-4v6yS6AI5VpBDgjK7y7TUQ@public.gmane.org,
benoit.cousson-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org,
rnayak-l0cyMroinI0@public.gmane.org,
peter.ujfalusi-l0cyMroinI0@public.gmane.org,
kishon-l0cyMroinI0@public.gmane.org,
jic23-KWPb1pKIrIJaa/9Udqfwiw@public.gmane.org,
grant.likely-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org,
rob.herring-bsGFqQB8/DxBDgjK7y7TUQ@public.gmane.org,
sameo-VuQAYsv1563Yd54FQh9/CA@public.gmane.org,
ch.naveen-Sze3O3UU22JBDgjK7y7TUQ@public.gmane.org,
poeschel-Xtl8qvBWbHwb1SvskN2V4Q@public.gmane.org,
milo.kim-l0cyMroinI0@public.gmane.org,
balajitk-l0cyMroinI0@public.gmane.org,
gg-kDsPt+C1G03kYMGBc/C6ZA@public.gmane.org,
linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r@public.gmane.org,
linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
linux-iio-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
devicetree-discuss-uLR06cmDAlY/bJ5BZ2RsiQ@public.gmane.org,
linux-omap-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
Subject: Re: [PATCH v5 2/2] iio: twl6030-gpadc: TWL6030, TWL6032 GPADC driver
Date: Fri, 19 Jul 2013 11:35:41 +0300 [thread overview]
Message-ID: <CAPb7_ms36YnT2VVkhngCEonwihtgHSMWO0uQQxsr58rUCgRPLA@mail.gmail.com> (raw)
In-Reply-To: <51E7B9DA.9040803-Qo5EllUWu/uELgA04lAiVw@public.gmane.org>
Hello Lars,
On Thu, Jul 18, 2013 at 12:48 PM, Lars-Peter Clausen <lars-Qo5EllUWu/uELgA04lAiVw@public.gmane.org> wrote:
> On 07/18/2013 10:36 AM, Oleksandr Kozaruk wrote:
>> Hello Lars,
>>
>> On Wed, Jul 17, 2013 at 9:04 PM, Lars-Peter Clausen <lars-Qo5EllUWu/uELgA04lAiVw@public.gmane.org> wrote:
>>>> +static int twl6032_calibration(struct twl6030_gpadc_data *gpadc)
>>>> +{
>>>> + int chn, d1 = 0, d2 = 0, temp;
>>>> + u8 trim_regs[17];
>>>> + int ret;
>>>> +
>>>> + ret = twl_i2c_read(TWL6030_MODULE_ID2, trim_regs + 1,
>>>> + TWL6030_GPADC_TRIM1, 16);
>>>> + if (ret < 0) {
>>>> + dev_err(gpadc->dev, "calibration failed\n");
>>>> + return ret;
>>>> + }
>>>> +
>>>> + /*
>>>> + * Loop to calculate the value needed for returning voltages from
>>>> + * GPADC not values.
>>>> + *
>>>> + * gain is calculated to 3 decimal places fixed point.
>>>> + */
>>>> + for (chn = 0; chn < TWL6032_GPADC_MAX_CHANNELS; chn++) {
>>>> +
>>>> + switch (chn) {
>>>> + case 0:
>>>> + case 1:
>>>> + case 2:
>>>> + case 3:
>>>> + case 4:
>>>> + case 5:
>>>> + case 6:
>>>> + case 11:
>>>> + case 12:
>>>> + case 13:
>>>> + case 14:
>>>> + /* D1 */
>>>> + d1 = (trim_regs[3] & 0x1F) << 2;
>>>> + d1 |= (trim_regs[1] & 0x06) >> 1;
>>>> + if (trim_regs[1] & 0x01)
>>>> + d1 = -d1;
>>>> +
>>>> + /* D2 */
>>>> + d2 = (trim_regs[4] & 0x3F) << 2;
>>>> + d2 |= (trim_regs[2] & 0x06) >> 1;
>>>> + if (trim_regs[2] & 0x01)
>>>> + d2 = -d2;
>>>> + break;
>>>> + case 8:
>>>> + /* D1 */
>>>> + temp = (trim_regs[3] & 0x1F) << 2;
>>>> + temp |= (trim_regs[1] & 0x06) >> 1;
>>>> + if (trim_regs[1] & 0x01)
>>>> + temp = -temp;
>>>> +
>>>> + d1 = (trim_regs[8] & 0x18) << 1;
>>>> + d1 |= (trim_regs[7] & 0x1E) >> 1;
>>>> + if (trim_regs[7] & 0x01)
>>>> + d1 = -d1;
>>>> +
>>>> + d1 += temp;
>>>> +
>>>> + /* D2 */
>>>> + temp = (trim_regs[4] & 0x3F) << 2;
>>>> + temp |= (trim_regs[2] & 0x06) >> 1;
>>>> + if (trim_regs[2] & 0x01)
>>>> + temp = -temp;
>>>> +
>>>> + d2 = (trim_regs[10] & 0x1F) << 2;
>>>> + d2 |= (trim_regs[8] & 0x06) >> 1;
>>>> + if (trim_regs[8] & 0x01)
>>>> + d2 = -d2;
>>>> +
>>>> + d2 += temp;
>>>> + break;
>>>> + case 9:
>>>> + /* D1 */
>>>> + temp = (trim_regs[3] & 0x1F) << 2;
>>>> + temp |= (trim_regs[1] & 0x06) >> 1;
>>>> + if (trim_regs[1] & 0x01)
>>>> + temp = -temp;
>>>> +
>>>> + d1 = (trim_regs[14] & 0x18) << 1;
>>>> + d1 |= (trim_regs[12] & 0x1E) >> 1;
>>>> + if (trim_regs[12] & 0x01)
>>>> + d1 = -d1;
>>>> +
>>>> + d1 += temp;
>>>> +
>>>> + /* D2 */
>>>> + temp = (trim_regs[4] & 0x3F) << 2;
>>>> + temp |= (trim_regs[2] & 0x06) >> 1;
>>>> + if (trim_regs[2] & 0x01)
>>>> + temp = -temp;
>>>> +
>>>> + d2 = (trim_regs[16] & 0x1F) << 2;
>>>> + d2 |= (trim_regs[14] & 0x06) >> 1;
>>>> + if (trim_regs[14] & 0x01)
>>>> + d2 = -d2;
>>>> +
>>>> + d2 += temp;
>>>> + case 10:
>>>> + /* D1 */
>>>> + d1 = (trim_regs[11] & 0x0F) << 3;
>>>> + d1 |= (trim_regs[9] & 0x0E) >> 1;
>>>> + if (trim_regs[9] & 0x01)
>>>> + d1 = -d1;
>>>> +
>>>> + /* D2 */
>>>> + d2 = (trim_regs[15] & 0x0F) << 3;
>>>> + d2 |= (trim_regs[13] & 0x0E) >> 1;
>>>> + if (trim_regs[13] & 0x01)
>>>> + d2 = -d2;
>>>> + break;
>>>> + case 7:
>>>> + case 18:
>>>> + /* D1 */
>>>> + temp = (trim_regs[3] & 0x1F) << 2;
>>>> + temp |= (trim_regs[1] & 0x06) >> 1;
>>>> + if (trim_regs[1] & 0x01)
>>>> + temp = -temp;
>>>> +
>>>> + d1 = (trim_regs[5] & 0x7E) >> 1;
>>>> + if (trim_regs[5] & 0x01)
>>>> + d1 = -d1;
>>>> +.
>>>> + d1 += temp;
>>>> +
>>>> + /* D2 */
>>>> + temp = (trim_regs[4] & 0x3F) << 2;
>>>> + temp |= (trim_regs[2] & 0x06) >> 1;
>>>> + if (trim_regs[2] & 0x01)
>>>> + temp = -temp;
>>>> +
>>>> + d2 = (trim_regs[6] & 0xFE) >> 1;
>>>> + if (trim_regs[6] & 0x01)
>>>> + d2 = -d2;
>>>> +
>>>> + d2 += temp;
>>>> + break;
>>>
>>>
>>> There is quite a bit of copy paste in here. Putting the bit swizziling into
>>> a helper function should also improbe legibility.
>>
>> Trimming bits are scattered randomly between registers and in
>> different bit positions.
>> I don't see any commonalities, that could be generalized. as Graeme
>> said about it "it's a bane of my life".
>
> The pattern is pretty regular though, how about:
>
>
> int twl6032_get_trim_value(u8 *trim_regs, unsigned int reg0,
> unsigned int reg1, unsigned int mask0, unsigned int mask1,
> unsigned int shift0)
> {
> int val;
>
> val = (trim_regs[reg0] & mask0) << shift0;
> val |= (trim_regs[reg1] & mask1) >> 1;
> if (trim_regs[reg1] & 0x01)
> val = -val;
>
> return val;
> }
>
> and then
>
> d1 = twl6032_get_trim_value(trim_regs, 3, 1, 0x1f, 0x06, 2);
> d2 = twl6032_get_trim_value(trim_regs, 4, 2, 0x3f, 0x06, 2);
>
> and so on.
Thank you. I think legibility is better now, I even found a bug - in
case 9 'break' was missing.
Though it is not without special case for channels 7, 18:
for (chn = 0; chn < TWL6032_GPADC_MAX_CHANNELS; chn++) {
switch (chn) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 11:
case 12:
case 13:
case 14:
d1 = twl6032_get_trim_value(trim_regs, 3, 1, 0x1f, 0x06, 2);
d2 = twl6032_get_trim_value(trim_regs, 4, 2, 0x3f, 0x06, 2);
break;
case 8:
temp = twl6032_get_trim_value(trim_regs, 3, 1, 0x1f, 0x06, 2);
d1 = temp + twl6032_get_trim_value(trim_regs, 8, 7, 0x18, 0x1E, 1);
temp = twl6032_get_trim_value(trim_regs, 4, 2, 0x3F, 0x06, 2);
d2 = temp + twl6032_get_trim_value(trim_regs, 10, 8, 0x1F, 0x06, 2);
break;
case 9:
temp = twl6032_get_trim_value(trim_regs, 3, 1, 0x1f, 0x06, 2);
d1 = temp + twl6032_get_trim_value(trim_regs, 14, 12, 0x18, 0x1E, 1);
temp = twl6032_get_trim_value(trim_regs, 4, 2, 0x3f, 0x06, 2);
d2 = temp + twl6032_get_trim_value(trim_regs, 16, 14, 0x1F, 0x06, 1);
break;
case 10:
d1 = twl6032_get_trim_value(trim_regs, 11, 9, 0x0f, 0x0E, 3);
d2 = twl6032_get_trim_value(trim_regs, 15, 13, 0x0f, 0x0E, 3);
break;
case 7:
case 18:
temp = twl6032_get_trim_value(trim_regs, 3, 1, 0x1f, 0x06, 2);
d1 = (trim_regs[5] & 0x7E) >> 1;
if (trim_regs[5] & 0x01)
d1 = -d1;
d1 += temp;
temp = twl6032_get_trim_value(trim_regs, 4, 2, 0x3f, 0x06, 2);
d2 = (trim_regs[6] & 0xFE) >> 1;
if (trim_regs[6] & 0x01)
d2 = -d2;
d2 += temp;
break;
default:
/* No data for other channels */
continue;
}
--
Regards,
Oleksandr Kozaruk
prev parent reply other threads:[~2013-07-19 8:35 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-07-17 11:12 [PATCH v5 0/2] TWL6030, TWL6032 GPADC driver Oleksandr Kozaruk
2013-07-17 11:12 ` [PATCH v5 1/2] ARM: dts: twl: Add GPADC data to device tree Oleksandr Kozaruk
2013-07-17 14:33 ` Sergei Shtylyov
[not found] ` <51E6AB54.103-M4DtvfQ/ZS1MRgGoP+s0PdBPR1lH4CV8@public.gmane.org>
2013-07-17 14:48 ` Lars-Peter Clausen
[not found] ` <1374059564-6872-1-git-send-email-oleksandr.kozaruk-l0cyMroinI0@public.gmane.org>
2013-07-17 11:12 ` [PATCH v5 2/2] iio: twl6030-gpadc: TWL6030, TWL6032 GPADC driver Oleksandr Kozaruk
[not found] ` <1374059564-6872-3-git-send-email-oleksandr.kozaruk-l0cyMroinI0@public.gmane.org>
2013-07-17 18:04 ` Lars-Peter Clausen
2013-07-18 8:36 ` Oleksandr Kozaruk
[not found] ` <CAPb7_muAzO-jRqgcm89fcKT-hTLq38Aj2p0WAWjGjN9DoQUwPg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2013-07-18 9:48 ` Lars-Peter Clausen
[not found] ` <51E7B9DA.9040803-Qo5EllUWu/uELgA04lAiVw@public.gmane.org>
2013-07-19 8:35 ` Oleksandr Kozaruk [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAPb7_ms36YnT2VVkhngCEonwihtgHSMWO0uQQxsr58rUCgRPLA@mail.gmail.com \
--to=oleksandr.kozaruk-hexfymnmjl/cnp4w7fqmdg@public.gmane.org \
--cc=balajitk-l0cyMroinI0@public.gmane.org \
--cc=benoit.cousson-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org \
--cc=ch.naveen-Sze3O3UU22JBDgjK7y7TUQ@public.gmane.org \
--cc=devicetree-discuss-uLR06cmDAlY/bJ5BZ2RsiQ@public.gmane.org \
--cc=gg-kDsPt+C1G03kYMGBc/C6ZA@public.gmane.org \
--cc=grant.likely-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org \
--cc=jic23-KWPb1pKIrIJaa/9Udqfwiw@public.gmane.org \
--cc=kishon-l0cyMroinI0@public.gmane.org \
--cc=lars-Qo5EllUWu/uELgA04lAiVw@public.gmane.org \
--cc=linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r@public.gmane.org \
--cc=linux-iio-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=linux-omap-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=milo.kim-l0cyMroinI0@public.gmane.org \
--cc=peter.ujfalusi-l0cyMroinI0@public.gmane.org \
--cc=poeschel-Xtl8qvBWbHwb1SvskN2V4Q@public.gmane.org \
--cc=rnayak-l0cyMroinI0@public.gmane.org \
--cc=rob.herring-bsGFqQB8/DxBDgjK7y7TUQ@public.gmane.org \
--cc=sameo-VuQAYsv1563Yd54FQh9/CA@public.gmane.org \
--cc=tony-4v6yS6AI5VpBDgjK7y7TUQ@public.gmane.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).