CINXE.COM
LKML: James Chapman: 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: James Chapman: 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 James Chapman" href="/groupie.php?aid=28360" /><!--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/206" onclick="this.href='/lkml/headers'+'/2005/4/8/206';">[headers]</a>聽 <a href="/lkml/bounce/2005/4/8/206">[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/8/104">"Jean Delvare"</a><ul><li><a href="/lkml/2005/4/8/124">Ladislav Michl</a><ul><li><a href="/lkml/2005/4/8/182">"Jean Delvare"</a></li><li class="origin"><a href="/lkml/2005/4/10/130">James Chapman</a><ul><li><a href="/lkml/2005/4/10/130">Ladislav Michl</a><ul><li><a href="/lkml/2005/4/10/143">Jean Delvare</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, 08 Apr 2005 18:44:53 +0100</td></tr><tr><td class="lp">From</td><td class="rp" itemprop="author">James Chapman <></td></tr><tr><td class="lp">Subject</td><td class="rp" itemprop="name">Re: [PATCH] ds1337 4/4</td></tr></table></td><td></td></tr></table><pre itemprop="articleBody">Ladislav Michl wrote:<br /><br />> On Fri, Apr 08, 2005 at 01:08:46PM +0200, Jean Delvare wrote:<br />> <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 />> <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 />> <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 />> <br />> Disabling battery charge makes no sense. <br /><br />If it causes more power to be drawn unnecessarily then battery charge <br />should be enabled only when needed.<br /><br /> > 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 />Why not add a new /sys entry for it? Is there a generic battery charge <br />control /sys API?<br /><br />> Golden rule is: implement features as needed :)<br /><br />But when adding code, try to cover all reasonable cases, otherwise we'll <br />see patches from people trying to add platform specific ifdefs in here.<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 />> <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 />I agree with Jean. You should provide an API for this. Don't take <br />shortcuts just because you were the first to support the chip. It'll be <br />more useful to others if you provide a way to set a value per platform.<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 />Just because it runs overnight doesn't mean there's no bug.<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 />> <br />> Eh? Register is 8bit, that's 256 combinations.<br /><br />Reserved bits have fixed values that you can test for.<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. <br /><br />I agree.<br /><br />I also don't know what a DS1337 will answer for this<br />>>missing register. Maybe James can help?<br /><br />I don't know. If I can find some time, I'll apply your patches and find out.<br /><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 />There are lots of i2c devices which differ only by one or two registers.<br />You are modifying this driver to report the new device so the device <br />detection must be reliable.<br /><br />The problem here is how to distinguish the difference between a read of <br />ds1339's battery register 0x10 and a ds1337's register 0x00.<br /><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 />I don't understand what you are trying to say.<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 />> <br />> You will change register 00h.<br /><br />Is writing 0x80 to register 0x10 valid for the ds1339?<br /><br />This might be a good test but should be verified. According to ds1337's <br />docs, writing 0x80 to register 0x00 would try to set a reserved bit so <br />should read back 0 on a ds1337. But docs can be wrong and trying to set <br />that bit might cause problems. I'll try to find out on my hardware.<br /><br />/james<br /><br /><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 19:52 聽聽 [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>