CINXE.COM
LKML: Ladislav Michl: Re: [PATCH] ds1337 4/4
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>LKML: Ladislav Michl: Re: [PATCH] ds1337 4/4</title><link href="/css/message.css" rel="stylesheet" type="text/css" /><link href="/css/wrap.css" rel="alternate stylesheet" type="text/css" title="wrap" /><link href="/css/nowrap.css" rel="stylesheet" type="text/css" title="nowrap" /><link href="/favicon.ico" rel="shortcut icon" /><script src="/js/simple-calendar.js" type="text/javascript"></script><script src="/js/styleswitcher.js" type="text/javascript"></script><link rel="alternate" type="application/rss+xml" title="lkml.org : last 100 messages" href="/rss.php" /><link rel="alternate" type="application/rss+xml" title="lkml.org : last messages by Ladislav Michl" href="/groupie.php?aid=21763" /><!--Matomo--><script> var _paq = window._paq = window._paq || []; /* tracker methods like "setCustomDimension" should be called before "trackPageView" */ _paq.push(["setDoNotTrack", true]); _paq.push(["disableCookies"]); _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function() { var u="//m.lkml.org/"; _paq.push(['setTrackerUrl', u+'matomo.php']); _paq.push(['setSiteId', '1']); var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); })(); </script><!--End Matomo Code--></head><body onload="es.jasper.simpleCalendar.init();" itemscope="itemscope" itemtype="http://schema.org/BlogPosting"><table border="0" cellpadding="0" cellspacing="0"><tr><td width="180" align="center"><a href="/"><img style="border:0;width:135px;height:32px" src="/images/toprowlk.gif" alt="lkml.org" /></a></td><td width="32">聽</td><td class="nb"><div><a class="nb" href="/lkml"> [lkml]</a> 聽 <a class="nb" href="/lkml/2005"> [2005]</a> 聽 <a class="nb" href="/lkml/2005/4"> [Apr]</a> 聽 <a class="nb" href="/lkml/2005/4/8"> [8]</a> 聽 <a class="nb" href="/lkml/last100"> [last100]</a> 聽 <a href="/rss.php"><img src="/images/rss-or.gif" border="0" alt="RSS Feed" /></a></div><div>Views: <a href="#" class="nowrap" onclick="setActiveStyleSheet('wrap');return false;">[wrap]</a><a href="#" class="wrap" onclick="setActiveStyleSheet('nowrap');return false;">[no wrap]</a> 聽 <a class="nb" href="/lkml/mheaders/2005/4/8/124" onclick="this.href='/lkml/headers'+'/2005/4/8/124';">[headers]</a>聽 <a href="/lkml/bounce/2005/4/8/124">[forward]</a>聽 </div></td><td width="32">聽</td></tr><tr><td valign="top"><div class="es-jasper-simpleCalendar" baseurl="/lkml/"></div><div class="threadlist">Messages in this thread</div><ul class="threadlist"><li class="root"><a href="/lkml/2005/3/31/279">First message in thread</a></li><li><a href="/lkml/2005/4/7/271">Ladislav Michl</a><ul><li><a href="/lkml/2005/4/8/104">"Jean Delvare"</a><ul><li class="origin"><a href="/lkml/2005/4/8/182">Ladislav Michl</a><ul><li><a href="/lkml/2005/4/8/182">"Jean Delvare"</a></li><li><a href="/lkml/2005/4/8/206">James Chapman</a><ul><li><a href="/lkml/2005/4/10/130">Ladislav Michl</a></li></ul></li></ul></li></ul></li></ul></li></ul></td><td width="32" rowspan="2" class="c" valign="top"><img src="/images/icornerl.gif" width="32" height="32" alt="/" /></td><td class="c" rowspan="2" valign="top" style="padding-top: 1em"><table><tr><td><table><tr><td class="lp">Date</td><td class="rp" itemprop="datePublished">Fri, 8 Apr 2005 14:35:45 +0200</td></tr><tr><td class="lp">Subject</td><td class="rp" itemprop="name">Re: [PATCH] ds1337 4/4</td></tr><tr><td class="lp">From</td><td class="rp" itemprop="author">Ladislav Michl <></td></tr></table></td><td></td></tr></table><pre itemprop="articleBody">On Fri, Apr 08, 2005 at 01:08:46PM +0200, Jean Delvare wrote:<br />> > Add support for DS1339. The only difference against DS1337 is Trickle<br />> > Charge register at address 10h, which is used to enable battery or gold<br />> > cap charging. Please note that value may vary for different batteries,<br />> > so it should be made module parameter. 0xaa is sane default and also<br />> > matches my board ;-)<br />> <br />> "Sane default" is a non-sense here. A sane default is that loading a<br />> real-time clock driver should not affect the battery at all IMHO.<br />><br />> Can you tell us more on that battery thing? I admit I don't exatcly get<br />> what it is. Which type of battery are we talking about? Are there<br />> similar drivers in the kernel tree already?<br /><br />Sorry, I have no clue what devices are using it and what may come to<br />board designer's mind. This chip will be most likely used in embedded,<br />where every sane developer is expected to review drivers he is using.<br />Also in such situation driver is compiled staticaly. (And in ideal world<br />firmware (such as U-Boot, RedBoot, etc.) should set this register).<br /><br />> Sounds weird to me that loading a driver would enable charging of a<br />> battery, and removing it wouldn't disable it. And since the driver<br />> might not be removed, it would possibly make more sense to have an entry<br />> in /sys to enable and disable this thing.<br /><br />Disabling battery charge makes no sense. The only reason I can think<br />about is when suspending device, so it is likely pm job. /sys entry<br />might help as well, but I do not see any point making driver more<br />complicated and bigger just to make someone else happy.<br /><br />Golden rule is: implement features as needed :)<br /><br />> Also, arbitrarily picking one of the 6 possible charging modes just<br />> because it matches your board is a bad idea. It looks like a value which<br />> should be set on a per-board basis, rather than picked randomly by the<br />> user, so as to avoid accidental hardware breakage.<br /><br />Well free to provide that way, so far I'm the only user so I did what is<br />usefull for me [*]. Everyone is welcome to change it to more generic<br />way.<br /><br />> > +static int ds1339_charge = 0xaa;<br />> <br />> I see little reason why this would be a global variable rather than a<br />> define (let alone the fact that this shouldn't be hardcoded at all<br />> IMHO, as I just explained).<br /><br />[*] This way it can be easily turned into module parameter or whatever<br />someone who will need different value may want to do.<br /><br />Lets choose define, later it can be converted to module parameter.<br />Gcc is smart enough to optimize charge enable code away.<br />#define ds1339_charge 0<br /><br />> > +/*<br />> > + * DS1339 has Trickle Charge register at address 10h. During a multibyte<br />> > + * access, when the address pointer reaches the end of the register space,<br />> > + * it wraps around to location 00h.<br />> > + * We read 17 bytes from device and compare first and last one. If they<br />> > + * are same it is most likely DS1337 chip.<br />> > + */<br />> > +static int ds1337_is_ds1339(struct i2c_client *client)<br />> > +{<br />> > + char buf[17], addr = 0;<br />> > + struct i2c_msg msg[2];<br />> > + int result;<br />> > +<br />> > + msg[0].addr = client->addr;<br />> > + msg[0].flags = 0;<br />> > + msg[0].len = 1;<br />> > + msg[0].buf = &addr;<br />> > +<br />> > + msg[1].addr = client->addr;<br />> > + msg[1].flags = I2C_M_RD;<br />> > + msg[1].len = sizeof(buf);<br />> > + msg[1].buf = buf;<br />> > +<br />> > + result = i2c_transfer(client->adapter, msg, 2);<br />> > + if (result < 0) {<br />> > + dev_err(&client->dev, "error reading data! %d\n", result);<br />> > + return 0;<br />> > + } else<br />> > + return (buf[0] == buf[16]) ? 0 : 1;<br />> > +}<br />> <br />> This will fail eventually. The first register is the seconds count, which<br />> by definition is changing. I2C is slow, by the time you wrap over the<br />> register range, the value could have changed. The datasheet explicitely<br />> says that the register cache will refresh on address wrapping.<br /><br />I was running test overnight and didn't meet any single case when this<br />happen. Perhaps device also needs to see start condition?<br /><br />> Also, 0x00 is a possible value for both the seconds count and the battery<br />> register, so you could miss a DS1339 at times.<br />> <br />> One possible check to start with would be on the value of the additional<br />> register itself. It has only 7 possible values. (...) <br /><br />Eh? Register is 8bit, that's 256 combinations.<br /><br />> (...) But of course it would be better if we could default to a DS1337<br />> and find a way to identify the DS1339, rather than the (unsafe) other<br />> way around. I also don't know what a DS1337 will answer for this<br />> missing register. Maybe James can help?<br />><br />> One possibility would be to start reading at 0x0E instead of 0x00,<br />> because register 0x00 is the control register and is the only one which<br />> will not change in our back as far as I can see. Oh, and the additional<br />> battery register too. But this still doesn't sound like a bulletproof<br />> method to me (we depend on the seconds and possibly minutes count<br />> again). So it would be better to additionally perform the same tests<br />> that were done on the non-wrapped registers for the regular DS1337<br />> detection, but on the wrapped area.<br />> <br />> The problem here is that all this will significantly increase the<br />> detection delay.<br /><br />That's probably overkill, see above.<br /><br />Look, the only difference between ds1339 and ds1337 is Trickle Charge<br />register. We won't touch it by default and if anyone wants to use it, he<br />need to provide its value. In that case he also knows it is DS1339 and<br />also knows what battery is wired, so he knows charging current.<br /><br />I hope it is also clear that without picking one "sane default" there is<br />no point to do any detection at all.<br /><br />> Yet another method would be to write a non-significant value to the<br />> battery register, such as 0x80. If we can read it back then it has to be<br />> a DS1339. But what effect will it have on the DS1337? I'd hope none,<br />> but this better be verified. And in general I don't much like using<br />> register writes in detection methods.<br /><br />You will change register 00h.<br /><br />> Could you please provide the output of i2cdump in b and c modes for your<br />> DS1339 chip? This might help finding an detection method. A similar dump<br />> for the DS1337 would help too.<br /><br />Sorry it seems there is no easy way to cross-compile this package. I<br />have admit I haven't tried too hard.<br /><br />> > - const char *name = "";<br />> > + const char *name;<br />> (...)<br />> > + default:<br />> > + name = "";<br />> > + }<br />> <br />> This is noise.<br /><br />No it is not. Look at generated machine code for each case and try to do<br />it for at least two architectures different from i386 ;-) I'm bored with<br />arguing about such changes, do what you think is right, but it will<br />remain in patch. The rest is up to you.<br /><br />Also note that this very way of rejecting tiny cleanups leads to hardly<br />readable and unoptimized code and after some greater amount of time code<br />rewrite occurs anyway (once someone is sufficienly upset).<br /><br />Have a nice weekend,<br /> ladis<br />-<br />To unsubscribe from this list: send the line "unsubscribe linux-kernel" in<br />the body of a message to majordomo@vger.kernel.org<br />More majordomo info at <a href="http://vger.kernel.org/majordomo-info.html">http://vger.kernel.org/majordomo-info.html</a><br />Please read the FAQ at <a href="http://www.tux.org/lkml/">http://www.tux.org/lkml/</a><br /><br /></pre></td><td width="32" rowspan="2" class="c" valign="top"><img src="/images/icornerr.gif" width="32" height="32" alt="\" /></td></tr><tr><td align="right" valign="bottom"> 聽 </td></tr><tr><td align="right" valign="bottom">聽</td><td class="c" valign="bottom" style="padding-bottom: 0px"><img src="/images/bcornerl.gif" width="32" height="32" alt="\" /></td><td class="c">聽</td><td class="c" valign="bottom" style="padding-bottom: 0px"><img src="/images/bcornerr.gif" width="32" height="32" alt="/" /></td></tr><tr><td align="right" valign="top" colspan="2"> 聽 </td><td class="lm">Last update: 2005-04-08 14:39 聽聽 [from the cache]<br />漏2003-2020 <a href="http://blog.jasper.es/"><span itemprop="editor">Jasper Spaans</span></a>|hosted at <a href="https://www.digitalocean.com/?refcode=9a8e99d24cf9">Digital Ocean</a> and my Meterkast|<a href="http://blog.jasper.es/categories.html#lkml-ref">Read the blog</a></td><td>聽</td></tr></table><script language="javascript" src="/js/styleswitcher.js" type="text/javascript"></script></body></html>