rtc-ds1307.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691
  1. /*
  2. * rtc-ds1307.c - RTC driver for some mostly-compatible I2C chips.
  3. *
  4. * Copyright (C) 2005 James Chapman (ds1337 core)
  5. * Copyright (C) 2006 David Brownell
  6. * Copyright (C) 2009 Matthias Fuchs (rx8025 support)
  7. * Copyright (C) 2012 Bertrand Achard (nvram access fixes)
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/bcd.h>
  14. #include <linux/i2c.h>
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/rtc/ds1307.h>
  18. #include <linux/rtc.h>
  19. #include <linux/slab.h>
  20. #include <linux/string.h>
  21. #include <linux/hwmon.h>
  22. #include <linux/hwmon-sysfs.h>
  23. #include <linux/clk-provider.h>
  24. /*
  25. * We can't determine type by probing, but if we expect pre-Linux code
  26. * to have set the chip up as a clock (turning on the oscillator and
  27. * setting the date and time), Linux can ignore the non-clock features.
  28. * That's a natural job for a factory or repair bench.
  29. */
  30. enum ds_type {
  31. ds_1307,
  32. ds_1337,
  33. ds_1338,
  34. ds_1339,
  35. ds_1340,
  36. ds_1388,
  37. ds_3231,
  38. m41t00,
  39. mcp794xx,
  40. rx_8025,
  41. last_ds_type /* always last */
  42. /* rs5c372 too? different address... */
  43. };
  44. /* RTC registers don't differ much, except for the century flag */
  45. #define DS1307_REG_SECS 0x00 /* 00-59 */
  46. # define DS1307_BIT_CH 0x80
  47. # define DS1340_BIT_nEOSC 0x80
  48. # define MCP794XX_BIT_ST 0x80
  49. #define DS1307_REG_MIN 0x01 /* 00-59 */
  50. #define DS1307_REG_HOUR 0x02 /* 00-23, or 1-12{am,pm} */
  51. # define DS1307_BIT_12HR 0x40 /* in REG_HOUR */
  52. # define DS1307_BIT_PM 0x20 /* in REG_HOUR */
  53. # define DS1340_BIT_CENTURY_EN 0x80 /* in REG_HOUR */
  54. # define DS1340_BIT_CENTURY 0x40 /* in REG_HOUR */
  55. #define DS1307_REG_WDAY 0x03 /* 01-07 */
  56. # define MCP794XX_BIT_VBATEN 0x08
  57. #define DS1307_REG_MDAY 0x04 /* 01-31 */
  58. #define DS1307_REG_MONTH 0x05 /* 01-12 */
  59. # define DS1337_BIT_CENTURY 0x80 /* in REG_MONTH */
  60. #define DS1307_REG_YEAR 0x06 /* 00-99 */
  61. /*
  62. * Other registers (control, status, alarms, trickle charge, NVRAM, etc)
  63. * start at 7, and they differ a LOT. Only control and status matter for
  64. * basic RTC date and time functionality; be careful using them.
  65. */
  66. #define DS1307_REG_CONTROL 0x07 /* or ds1338 */
  67. # define DS1307_BIT_OUT 0x80
  68. # define DS1338_BIT_OSF 0x20
  69. # define DS1307_BIT_SQWE 0x10
  70. # define DS1307_BIT_RS1 0x02
  71. # define DS1307_BIT_RS0 0x01
  72. #define DS1337_REG_CONTROL 0x0e
  73. # define DS1337_BIT_nEOSC 0x80
  74. # define DS1339_BIT_BBSQI 0x20
  75. # define DS3231_BIT_BBSQW 0x40 /* same as BBSQI */
  76. # define DS1337_BIT_RS2 0x10
  77. # define DS1337_BIT_RS1 0x08
  78. # define DS1337_BIT_INTCN 0x04
  79. # define DS1337_BIT_A2IE 0x02
  80. # define DS1337_BIT_A1IE 0x01
  81. #define DS1340_REG_CONTROL 0x07
  82. # define DS1340_BIT_OUT 0x80
  83. # define DS1340_BIT_FT 0x40
  84. # define DS1340_BIT_CALIB_SIGN 0x20
  85. # define DS1340_M_CALIBRATION 0x1f
  86. #define DS1340_REG_FLAG 0x09
  87. # define DS1340_BIT_OSF 0x80
  88. #define DS1337_REG_STATUS 0x0f
  89. # define DS1337_BIT_OSF 0x80
  90. # define DS3231_BIT_EN32KHZ 0x08
  91. # define DS1337_BIT_A2I 0x02
  92. # define DS1337_BIT_A1I 0x01
  93. #define DS1339_REG_ALARM1_SECS 0x07
  94. #define DS13XX_TRICKLE_CHARGER_MAGIC 0xa0
  95. #define RX8025_REG_CTRL1 0x0e
  96. # define RX8025_BIT_2412 0x20
  97. #define RX8025_REG_CTRL2 0x0f
  98. # define RX8025_BIT_PON 0x10
  99. # define RX8025_BIT_VDET 0x40
  100. # define RX8025_BIT_XST 0x20
  101. struct ds1307 {
  102. u8 offset; /* register's offset */
  103. u8 regs[11];
  104. u16 nvram_offset;
  105. struct bin_attribute *nvram;
  106. enum ds_type type;
  107. unsigned long flags;
  108. #define HAS_NVRAM 0 /* bit 0 == sysfs file active */
  109. #define HAS_ALARM 1 /* bit 1 == irq claimed */
  110. struct i2c_client *client;
  111. struct rtc_device *rtc;
  112. s32 (*read_block_data)(const struct i2c_client *client, u8 command,
  113. u8 length, u8 *values);
  114. s32 (*write_block_data)(const struct i2c_client *client, u8 command,
  115. u8 length, const u8 *values);
  116. #ifdef CONFIG_COMMON_CLK
  117. struct clk_hw clks[2];
  118. #endif
  119. };
  120. struct chip_desc {
  121. unsigned alarm:1;
  122. u16 nvram_offset;
  123. u16 nvram_size;
  124. u16 trickle_charger_reg;
  125. u8 trickle_charger_setup;
  126. u8 (*do_trickle_setup)(struct i2c_client *, uint32_t, bool);
  127. };
  128. static u8 do_trickle_setup_ds1339(struct i2c_client *,
  129. uint32_t ohms, bool diode);
  130. static struct chip_desc chips[last_ds_type] = {
  131. [ds_1307] = {
  132. .nvram_offset = 8,
  133. .nvram_size = 56,
  134. },
  135. [ds_1337] = {
  136. .alarm = 1,
  137. },
  138. [ds_1338] = {
  139. .nvram_offset = 8,
  140. .nvram_size = 56,
  141. },
  142. [ds_1339] = {
  143. .alarm = 1,
  144. .trickle_charger_reg = 0x10,
  145. .do_trickle_setup = &do_trickle_setup_ds1339,
  146. },
  147. [ds_1340] = {
  148. .trickle_charger_reg = 0x08,
  149. },
  150. [ds_1388] = {
  151. .trickle_charger_reg = 0x0a,
  152. },
  153. [ds_3231] = {
  154. .alarm = 1,
  155. },
  156. [mcp794xx] = {
  157. .alarm = 1,
  158. /* this is battery backed SRAM */
  159. .nvram_offset = 0x20,
  160. .nvram_size = 0x40,
  161. },
  162. };
  163. static const struct i2c_device_id ds1307_id[] = {
  164. { "ds1307", ds_1307 },
  165. { "ds1337", ds_1337 },
  166. { "ds1338", ds_1338 },
  167. { "ds1339", ds_1339 },
  168. { "ds1388", ds_1388 },
  169. { "ds1340", ds_1340 },
  170. { "ds3231", ds_3231 },
  171. { "m41t00", m41t00 },
  172. { "mcp7940x", mcp794xx },
  173. { "mcp7941x", mcp794xx },
  174. { "pt7c4338", ds_1307 },
  175. { "rx8025", rx_8025 },
  176. { "isl12057", ds_1337 },
  177. { }
  178. };
  179. MODULE_DEVICE_TABLE(i2c, ds1307_id);
  180. /*----------------------------------------------------------------------*/
  181. #define BLOCK_DATA_MAX_TRIES 10
  182. static s32 ds1307_read_block_data_once(const struct i2c_client *client,
  183. u8 command, u8 length, u8 *values)
  184. {
  185. s32 i, data;
  186. for (i = 0; i < length; i++) {
  187. data = i2c_smbus_read_byte_data(client, command + i);
  188. if (data < 0)
  189. return data;
  190. values[i] = data;
  191. }
  192. return i;
  193. }
  194. static s32 ds1307_read_block_data(const struct i2c_client *client, u8 command,
  195. u8 length, u8 *values)
  196. {
  197. u8 oldvalues[255];
  198. s32 ret;
  199. int tries = 0;
  200. dev_dbg(&client->dev, "ds1307_read_block_data (length=%d)\n", length);
  201. ret = ds1307_read_block_data_once(client, command, length, values);
  202. if (ret < 0)
  203. return ret;
  204. do {
  205. if (++tries > BLOCK_DATA_MAX_TRIES) {
  206. dev_err(&client->dev,
  207. "ds1307_read_block_data failed\n");
  208. return -EIO;
  209. }
  210. memcpy(oldvalues, values, length);
  211. ret = ds1307_read_block_data_once(client, command, length,
  212. values);
  213. if (ret < 0)
  214. return ret;
  215. } while (memcmp(oldvalues, values, length));
  216. return length;
  217. }
  218. static s32 ds1307_write_block_data(const struct i2c_client *client, u8 command,
  219. u8 length, const u8 *values)
  220. {
  221. u8 currvalues[255];
  222. int tries = 0;
  223. dev_dbg(&client->dev, "ds1307_write_block_data (length=%d)\n", length);
  224. do {
  225. s32 i, ret;
  226. if (++tries > BLOCK_DATA_MAX_TRIES) {
  227. dev_err(&client->dev,
  228. "ds1307_write_block_data failed\n");
  229. return -EIO;
  230. }
  231. for (i = 0; i < length; i++) {
  232. ret = i2c_smbus_write_byte_data(client, command + i,
  233. values[i]);
  234. if (ret < 0)
  235. return ret;
  236. }
  237. ret = ds1307_read_block_data_once(client, command, length,
  238. currvalues);
  239. if (ret < 0)
  240. return ret;
  241. } while (memcmp(currvalues, values, length));
  242. return length;
  243. }
  244. /*----------------------------------------------------------------------*/
  245. /* These RTC devices are not designed to be connected to a SMbus adapter.
  246. SMbus limits block operations length to 32 bytes, whereas it's not
  247. limited on I2C buses. As a result, accesses may exceed 32 bytes;
  248. in that case, split them into smaller blocks */
  249. static s32 ds1307_native_smbus_write_block_data(const struct i2c_client *client,
  250. u8 command, u8 length, const u8 *values)
  251. {
  252. u8 suboffset = 0;
  253. if (length <= I2C_SMBUS_BLOCK_MAX) {
  254. s32 retval = i2c_smbus_write_i2c_block_data(client,
  255. command, length, values);
  256. if (retval < 0)
  257. return retval;
  258. return length;
  259. }
  260. while (suboffset < length) {
  261. s32 retval = i2c_smbus_write_i2c_block_data(client,
  262. command + suboffset,
  263. min(I2C_SMBUS_BLOCK_MAX, length - suboffset),
  264. values + suboffset);
  265. if (retval < 0)
  266. return retval;
  267. suboffset += I2C_SMBUS_BLOCK_MAX;
  268. }
  269. return length;
  270. }
  271. static s32 ds1307_native_smbus_read_block_data(const struct i2c_client *client,
  272. u8 command, u8 length, u8 *values)
  273. {
  274. u8 suboffset = 0;
  275. if (length <= I2C_SMBUS_BLOCK_MAX)
  276. return i2c_smbus_read_i2c_block_data(client,
  277. command, length, values);
  278. while (suboffset < length) {
  279. s32 retval = i2c_smbus_read_i2c_block_data(client,
  280. command + suboffset,
  281. min(I2C_SMBUS_BLOCK_MAX, length - suboffset),
  282. values + suboffset);
  283. if (retval < 0)
  284. return retval;
  285. suboffset += I2C_SMBUS_BLOCK_MAX;
  286. }
  287. return length;
  288. }
  289. /*----------------------------------------------------------------------*/
  290. /*
  291. * The ds1337 and ds1339 both have two alarms, but we only use the first
  292. * one (with a "seconds" field). For ds1337 we expect nINTA is our alarm
  293. * signal; ds1339 chips have only one alarm signal.
  294. */
  295. static irqreturn_t ds1307_irq(int irq, void *dev_id)
  296. {
  297. struct i2c_client *client = dev_id;
  298. struct ds1307 *ds1307 = i2c_get_clientdata(client);
  299. struct mutex *lock = &ds1307->rtc->ops_lock;
  300. int stat, control;
  301. mutex_lock(lock);
  302. stat = i2c_smbus_read_byte_data(client, DS1337_REG_STATUS);
  303. if (stat < 0)
  304. goto out;
  305. if (stat & DS1337_BIT_A1I) {
  306. stat &= ~DS1337_BIT_A1I;
  307. i2c_smbus_write_byte_data(client, DS1337_REG_STATUS, stat);
  308. control = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL);
  309. if (control < 0)
  310. goto out;
  311. control &= ~DS1337_BIT_A1IE;
  312. i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL, control);
  313. rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
  314. }
  315. out:
  316. mutex_unlock(lock);
  317. return IRQ_HANDLED;
  318. }
  319. /*----------------------------------------------------------------------*/
  320. static int ds1307_get_time(struct device *dev, struct rtc_time *t)
  321. {
  322. struct ds1307 *ds1307 = dev_get_drvdata(dev);
  323. int tmp;
  324. /* read the RTC date and time registers all at once */
  325. tmp = ds1307->read_block_data(ds1307->client,
  326. ds1307->offset, 7, ds1307->regs);
  327. if (tmp != 7) {
  328. dev_err(dev, "%s error %d\n", "read", tmp);
  329. return -EIO;
  330. }
  331. dev_dbg(dev, "%s: %7ph\n", "read", ds1307->regs);
  332. t->tm_sec = bcd2bin(ds1307->regs[DS1307_REG_SECS] & 0x7f);
  333. t->tm_min = bcd2bin(ds1307->regs[DS1307_REG_MIN] & 0x7f);
  334. tmp = ds1307->regs[DS1307_REG_HOUR] & 0x3f;
  335. t->tm_hour = bcd2bin(tmp);
  336. t->tm_wday = bcd2bin(ds1307->regs[DS1307_REG_WDAY] & 0x07) - 1;
  337. t->tm_mday = bcd2bin(ds1307->regs[DS1307_REG_MDAY] & 0x3f);
  338. tmp = ds1307->regs[DS1307_REG_MONTH] & 0x1f;
  339. t->tm_mon = bcd2bin(tmp) - 1;
  340. t->tm_year = bcd2bin(ds1307->regs[DS1307_REG_YEAR]) + 100;
  341. #ifdef CONFIG_RTC_DRV_DS1307_CENTURY
  342. switch (ds1307->type) {
  343. case ds_1337:
  344. case ds_1339:
  345. case ds_3231:
  346. if (ds1307->regs[DS1307_REG_MONTH] & DS1337_BIT_CENTURY)
  347. t->tm_year += 100;
  348. break;
  349. case ds_1340:
  350. if (ds1307->regs[DS1307_REG_HOUR] & DS1340_BIT_CENTURY)
  351. t->tm_year += 100;
  352. break;
  353. default:
  354. break;
  355. }
  356. #endif
  357. dev_dbg(dev, "%s secs=%d, mins=%d, "
  358. "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
  359. "read", t->tm_sec, t->tm_min,
  360. t->tm_hour, t->tm_mday,
  361. t->tm_mon, t->tm_year, t->tm_wday);
  362. /* initial clock setting can be undefined */
  363. return rtc_valid_tm(t);
  364. }
  365. static int ds1307_set_time(struct device *dev, struct rtc_time *t)
  366. {
  367. struct ds1307 *ds1307 = dev_get_drvdata(dev);
  368. int result;
  369. int tmp;
  370. u8 *buf = ds1307->regs;
  371. dev_dbg(dev, "%s secs=%d, mins=%d, "
  372. "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
  373. "write", t->tm_sec, t->tm_min,
  374. t->tm_hour, t->tm_mday,
  375. t->tm_mon, t->tm_year, t->tm_wday);
  376. #ifdef CONFIG_RTC_DRV_DS1307_CENTURY
  377. if (t->tm_year < 100)
  378. return -EINVAL;
  379. switch (ds1307->type) {
  380. case ds_1337:
  381. case ds_1339:
  382. case ds_3231:
  383. case ds_1340:
  384. if (t->tm_year > 299)
  385. return -EINVAL;
  386. default:
  387. if (t->tm_year > 199)
  388. return -EINVAL;
  389. break;
  390. }
  391. #else
  392. if (t->tm_year < 100 || t->tm_year > 199)
  393. return -EINVAL;
  394. #endif
  395. buf[DS1307_REG_SECS] = bin2bcd(t->tm_sec);
  396. buf[DS1307_REG_MIN] = bin2bcd(t->tm_min);
  397. buf[DS1307_REG_HOUR] = bin2bcd(t->tm_hour);
  398. buf[DS1307_REG_WDAY] = bin2bcd(t->tm_wday + 1);
  399. buf[DS1307_REG_MDAY] = bin2bcd(t->tm_mday);
  400. buf[DS1307_REG_MONTH] = bin2bcd(t->tm_mon + 1);
  401. /* assume 20YY not 19YY */
  402. tmp = t->tm_year - 100;
  403. buf[DS1307_REG_YEAR] = bin2bcd(tmp);
  404. switch (ds1307->type) {
  405. case ds_1337:
  406. case ds_1339:
  407. case ds_3231:
  408. if (t->tm_year > 199)
  409. buf[DS1307_REG_MONTH] |= DS1337_BIT_CENTURY;
  410. break;
  411. case ds_1340:
  412. buf[DS1307_REG_HOUR] |= DS1340_BIT_CENTURY_EN;
  413. if (t->tm_year > 199)
  414. buf[DS1307_REG_HOUR] |= DS1340_BIT_CENTURY;
  415. break;
  416. case mcp794xx:
  417. /*
  418. * these bits were cleared when preparing the date/time
  419. * values and need to be set again before writing the
  420. * buffer out to the device.
  421. */
  422. buf[DS1307_REG_SECS] |= MCP794XX_BIT_ST;
  423. buf[DS1307_REG_WDAY] |= MCP794XX_BIT_VBATEN;
  424. break;
  425. default:
  426. break;
  427. }
  428. dev_dbg(dev, "%s: %7ph\n", "write", buf);
  429. result = ds1307->write_block_data(ds1307->client,
  430. ds1307->offset, 7, buf);
  431. if (result < 0) {
  432. dev_err(dev, "%s error %d\n", "write", result);
  433. return result;
  434. }
  435. return 0;
  436. }
  437. static int ds1337_read_alarm(struct device *dev, struct rtc_wkalrm *t)
  438. {
  439. struct i2c_client *client = to_i2c_client(dev);
  440. struct ds1307 *ds1307 = i2c_get_clientdata(client);
  441. int ret;
  442. if (!test_bit(HAS_ALARM, &ds1307->flags))
  443. return -EINVAL;
  444. /* read all ALARM1, ALARM2, and status registers at once */
  445. ret = ds1307->read_block_data(client,
  446. DS1339_REG_ALARM1_SECS, 9, ds1307->regs);
  447. if (ret != 9) {
  448. dev_err(dev, "%s error %d\n", "alarm read", ret);
  449. return -EIO;
  450. }
  451. dev_dbg(dev, "%s: %4ph, %3ph, %2ph\n", "alarm read",
  452. &ds1307->regs[0], &ds1307->regs[4], &ds1307->regs[7]);
  453. /*
  454. * report alarm time (ALARM1); assume 24 hour and day-of-month modes,
  455. * and that all four fields are checked matches
  456. */
  457. t->time.tm_sec = bcd2bin(ds1307->regs[0] & 0x7f);
  458. t->time.tm_min = bcd2bin(ds1307->regs[1] & 0x7f);
  459. t->time.tm_hour = bcd2bin(ds1307->regs[2] & 0x3f);
  460. t->time.tm_mday = bcd2bin(ds1307->regs[3] & 0x3f);
  461. /* ... and status */
  462. t->enabled = !!(ds1307->regs[7] & DS1337_BIT_A1IE);
  463. t->pending = !!(ds1307->regs[8] & DS1337_BIT_A1I);
  464. dev_dbg(dev, "%s secs=%d, mins=%d, "
  465. "hours=%d, mday=%d, enabled=%d, pending=%d\n",
  466. "alarm read", t->time.tm_sec, t->time.tm_min,
  467. t->time.tm_hour, t->time.tm_mday,
  468. t->enabled, t->pending);
  469. return 0;
  470. }
  471. static int ds1337_set_alarm(struct device *dev, struct rtc_wkalrm *t)
  472. {
  473. struct i2c_client *client = to_i2c_client(dev);
  474. struct ds1307 *ds1307 = i2c_get_clientdata(client);
  475. unsigned char *buf = ds1307->regs;
  476. u8 control, status;
  477. int ret;
  478. if (!test_bit(HAS_ALARM, &ds1307->flags))
  479. return -EINVAL;
  480. dev_dbg(dev, "%s secs=%d, mins=%d, "
  481. "hours=%d, mday=%d, enabled=%d, pending=%d\n",
  482. "alarm set", t->time.tm_sec, t->time.tm_min,
  483. t->time.tm_hour, t->time.tm_mday,
  484. t->enabled, t->pending);
  485. /* read current status of both alarms and the chip */
  486. ret = ds1307->read_block_data(client,
  487. DS1339_REG_ALARM1_SECS, 9, buf);
  488. if (ret != 9) {
  489. dev_err(dev, "%s error %d\n", "alarm write", ret);
  490. return -EIO;
  491. }
  492. control = ds1307->regs[7];
  493. status = ds1307->regs[8];
  494. dev_dbg(dev, "%s: %4ph, %3ph, %02x %02x\n", "alarm set (old status)",
  495. &ds1307->regs[0], &ds1307->regs[4], control, status);
  496. /* set ALARM1, using 24 hour and day-of-month modes */
  497. buf[0] = bin2bcd(t->time.tm_sec);
  498. buf[1] = bin2bcd(t->time.tm_min);
  499. buf[2] = bin2bcd(t->time.tm_hour);
  500. buf[3] = bin2bcd(t->time.tm_mday);
  501. /* set ALARM2 to non-garbage */
  502. buf[4] = 0;
  503. buf[5] = 0;
  504. buf[6] = 0;
  505. /* disable alarms */
  506. buf[7] = control & ~(DS1337_BIT_A1IE | DS1337_BIT_A2IE);
  507. buf[8] = status & ~(DS1337_BIT_A1I | DS1337_BIT_A2I);
  508. ret = ds1307->write_block_data(client,
  509. DS1339_REG_ALARM1_SECS, 9, buf);
  510. if (ret < 0) {
  511. dev_err(dev, "can't set alarm time\n");
  512. return ret;
  513. }
  514. /* optionally enable ALARM1 */
  515. if (t->enabled) {
  516. dev_dbg(dev, "alarm IRQ armed\n");
  517. buf[7] |= DS1337_BIT_A1IE; /* only ALARM1 is used */
  518. i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL, buf[7]);
  519. }
  520. return 0;
  521. }
  522. static int ds1307_alarm_irq_enable(struct device *dev, unsigned int enabled)
  523. {
  524. struct i2c_client *client = to_i2c_client(dev);
  525. struct ds1307 *ds1307 = i2c_get_clientdata(client);
  526. int ret;
  527. if (!test_bit(HAS_ALARM, &ds1307->flags))
  528. return -ENOTTY;
  529. ret = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL);
  530. if (ret < 0)
  531. return ret;
  532. if (enabled)
  533. ret |= DS1337_BIT_A1IE;
  534. else
  535. ret &= ~DS1337_BIT_A1IE;
  536. ret = i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL, ret);
  537. if (ret < 0)
  538. return ret;
  539. return 0;
  540. }
  541. static const struct rtc_class_ops ds13xx_rtc_ops = {
  542. .read_time = ds1307_get_time,
  543. .set_time = ds1307_set_time,
  544. .read_alarm = ds1337_read_alarm,
  545. .set_alarm = ds1337_set_alarm,
  546. .alarm_irq_enable = ds1307_alarm_irq_enable,
  547. };
  548. /*----------------------------------------------------------------------*/
  549. /*
  550. * Alarm support for mcp794xx devices.
  551. */
  552. #define MCP794XX_REG_WEEKDAY 0x3
  553. #define MCP794XX_REG_WEEKDAY_WDAY_MASK 0x7
  554. #define MCP794XX_REG_CONTROL 0x07
  555. # define MCP794XX_BIT_ALM0_EN 0x10
  556. # define MCP794XX_BIT_ALM1_EN 0x20
  557. #define MCP794XX_REG_ALARM0_BASE 0x0a
  558. #define MCP794XX_REG_ALARM0_CTRL 0x0d
  559. #define MCP794XX_REG_ALARM1_BASE 0x11
  560. #define MCP794XX_REG_ALARM1_CTRL 0x14
  561. # define MCP794XX_BIT_ALMX_IF (1 << 3)
  562. # define MCP794XX_BIT_ALMX_C0 (1 << 4)
  563. # define MCP794XX_BIT_ALMX_C1 (1 << 5)
  564. # define MCP794XX_BIT_ALMX_C2 (1 << 6)
  565. # define MCP794XX_BIT_ALMX_POL (1 << 7)
  566. # define MCP794XX_MSK_ALMX_MATCH (MCP794XX_BIT_ALMX_C0 | \
  567. MCP794XX_BIT_ALMX_C1 | \
  568. MCP794XX_BIT_ALMX_C2)
  569. static irqreturn_t mcp794xx_irq(int irq, void *dev_id)
  570. {
  571. struct i2c_client *client = dev_id;
  572. struct ds1307 *ds1307 = i2c_get_clientdata(client);
  573. struct mutex *lock = &ds1307->rtc->ops_lock;
  574. int reg, ret;
  575. mutex_lock(lock);
  576. /* Check and clear alarm 0 interrupt flag. */
  577. reg = i2c_smbus_read_byte_data(client, MCP794XX_REG_ALARM0_CTRL);
  578. if (reg < 0)
  579. goto out;
  580. if (!(reg & MCP794XX_BIT_ALMX_IF))
  581. goto out;
  582. reg &= ~MCP794XX_BIT_ALMX_IF;
  583. ret = i2c_smbus_write_byte_data(client, MCP794XX_REG_ALARM0_CTRL, reg);
  584. if (ret < 0)
  585. goto out;
  586. /* Disable alarm 0. */
  587. reg = i2c_smbus_read_byte_data(client, MCP794XX_REG_CONTROL);
  588. if (reg < 0)
  589. goto out;
  590. reg &= ~MCP794XX_BIT_ALM0_EN;
  591. ret = i2c_smbus_write_byte_data(client, MCP794XX_REG_CONTROL, reg);
  592. if (ret < 0)
  593. goto out;
  594. rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
  595. out:
  596. mutex_unlock(lock);
  597. return IRQ_HANDLED;
  598. }
  599. static int mcp794xx_read_alarm(struct device *dev, struct rtc_wkalrm *t)
  600. {
  601. struct i2c_client *client = to_i2c_client(dev);
  602. struct ds1307 *ds1307 = i2c_get_clientdata(client);
  603. u8 *regs = ds1307->regs;
  604. int ret;
  605. if (!test_bit(HAS_ALARM, &ds1307->flags))
  606. return -EINVAL;
  607. /* Read control and alarm 0 registers. */
  608. ret = ds1307->read_block_data(client, MCP794XX_REG_CONTROL, 10, regs);
  609. if (ret < 0)
  610. return ret;
  611. t->enabled = !!(regs[0] & MCP794XX_BIT_ALM0_EN);
  612. /* Report alarm 0 time assuming 24-hour and day-of-month modes. */
  613. t->time.tm_sec = bcd2bin(ds1307->regs[3] & 0x7f);
  614. t->time.tm_min = bcd2bin(ds1307->regs[4] & 0x7f);
  615. t->time.tm_hour = bcd2bin(ds1307->regs[5] & 0x3f);
  616. t->time.tm_wday = bcd2bin(ds1307->regs[6] & 0x7) - 1;
  617. t->time.tm_mday = bcd2bin(ds1307->regs[7] & 0x3f);
  618. t->time.tm_mon = bcd2bin(ds1307->regs[8] & 0x1f) - 1;
  619. t->time.tm_year = -1;
  620. t->time.tm_yday = -1;
  621. t->time.tm_isdst = -1;
  622. dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
  623. "enabled=%d polarity=%d irq=%d match=%d\n", __func__,
  624. t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
  625. t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled,
  626. !!(ds1307->regs[6] & MCP794XX_BIT_ALMX_POL),
  627. !!(ds1307->regs[6] & MCP794XX_BIT_ALMX_IF),
  628. (ds1307->regs[6] & MCP794XX_MSK_ALMX_MATCH) >> 4);
  629. return 0;
  630. }
  631. static int mcp794xx_set_alarm(struct device *dev, struct rtc_wkalrm *t)
  632. {
  633. struct i2c_client *client = to_i2c_client(dev);
  634. struct ds1307 *ds1307 = i2c_get_clientdata(client);
  635. unsigned char *regs = ds1307->regs;
  636. int ret;
  637. if (!test_bit(HAS_ALARM, &ds1307->flags))
  638. return -EINVAL;
  639. dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
  640. "enabled=%d pending=%d\n", __func__,
  641. t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
  642. t->time.tm_wday, t->time.tm_mday, t->time.tm_mon,
  643. t->enabled, t->pending);
  644. /* Read control and alarm 0 registers. */
  645. ret = ds1307->read_block_data(client, MCP794XX_REG_CONTROL, 10, regs);
  646. if (ret < 0)
  647. return ret;
  648. /* Set alarm 0, using 24-hour and day-of-month modes. */
  649. regs[3] = bin2bcd(t->time.tm_sec);
  650. regs[4] = bin2bcd(t->time.tm_min);
  651. regs[5] = bin2bcd(t->time.tm_hour);
  652. regs[6] = bin2bcd(t->time.tm_wday + 1);
  653. regs[7] = bin2bcd(t->time.tm_mday);
  654. regs[8] = bin2bcd(t->time.tm_mon + 1);
  655. /* Clear the alarm 0 interrupt flag. */
  656. regs[6] &= ~MCP794XX_BIT_ALMX_IF;
  657. /* Set alarm match: second, minute, hour, day, date, month. */
  658. regs[6] |= MCP794XX_MSK_ALMX_MATCH;
  659. /* Disable interrupt. We will not enable until completely programmed */
  660. regs[0] &= ~MCP794XX_BIT_ALM0_EN;
  661. ret = ds1307->write_block_data(client, MCP794XX_REG_CONTROL, 10, regs);
  662. if (ret < 0)
  663. return ret;
  664. if (!t->enabled)
  665. return 0;
  666. regs[0] |= MCP794XX_BIT_ALM0_EN;
  667. return i2c_smbus_write_byte_data(client, MCP794XX_REG_CONTROL, regs[0]);
  668. }
  669. static int mcp794xx_alarm_irq_enable(struct device *dev, unsigned int enabled)
  670. {
  671. struct i2c_client *client = to_i2c_client(dev);
  672. struct ds1307 *ds1307 = i2c_get_clientdata(client);
  673. int reg;
  674. if (!test_bit(HAS_ALARM, &ds1307->flags))
  675. return -EINVAL;
  676. reg = i2c_smbus_read_byte_data(client, MCP794XX_REG_CONTROL);
  677. if (reg < 0)
  678. return reg;
  679. if (enabled)
  680. reg |= MCP794XX_BIT_ALM0_EN;
  681. else
  682. reg &= ~MCP794XX_BIT_ALM0_EN;
  683. return i2c_smbus_write_byte_data(client, MCP794XX_REG_CONTROL, reg);
  684. }
  685. static const struct rtc_class_ops mcp794xx_rtc_ops = {
  686. .read_time = ds1307_get_time,
  687. .set_time = ds1307_set_time,
  688. .read_alarm = mcp794xx_read_alarm,
  689. .set_alarm = mcp794xx_set_alarm,
  690. .alarm_irq_enable = mcp794xx_alarm_irq_enable,
  691. };
  692. /*----------------------------------------------------------------------*/
  693. static ssize_t
  694. ds1307_nvram_read(struct file *filp, struct kobject *kobj,
  695. struct bin_attribute *attr,
  696. char *buf, loff_t off, size_t count)
  697. {
  698. struct i2c_client *client;
  699. struct ds1307 *ds1307;
  700. int result;
  701. client = kobj_to_i2c_client(kobj);
  702. ds1307 = i2c_get_clientdata(client);
  703. result = ds1307->read_block_data(client, ds1307->nvram_offset + off,
  704. count, buf);
  705. if (result < 0)
  706. dev_err(&client->dev, "%s error %d\n", "nvram read", result);
  707. return result;
  708. }
  709. static ssize_t
  710. ds1307_nvram_write(struct file *filp, struct kobject *kobj,
  711. struct bin_attribute *attr,
  712. char *buf, loff_t off, size_t count)
  713. {
  714. struct i2c_client *client;
  715. struct ds1307 *ds1307;
  716. int result;
  717. client = kobj_to_i2c_client(kobj);
  718. ds1307 = i2c_get_clientdata(client);
  719. result = ds1307->write_block_data(client, ds1307->nvram_offset + off,
  720. count, buf);
  721. if (result < 0) {
  722. dev_err(&client->dev, "%s error %d\n", "nvram write", result);
  723. return result;
  724. }
  725. return count;
  726. }
  727. /*----------------------------------------------------------------------*/
  728. static u8 do_trickle_setup_ds1339(struct i2c_client *client,
  729. uint32_t ohms, bool diode)
  730. {
  731. u8 setup = (diode) ? DS1307_TRICKLE_CHARGER_DIODE :
  732. DS1307_TRICKLE_CHARGER_NO_DIODE;
  733. switch (ohms) {
  734. case 250:
  735. setup |= DS1307_TRICKLE_CHARGER_250_OHM;
  736. break;
  737. case 2000:
  738. setup |= DS1307_TRICKLE_CHARGER_2K_OHM;
  739. break;
  740. case 4000:
  741. setup |= DS1307_TRICKLE_CHARGER_4K_OHM;
  742. break;
  743. default:
  744. dev_warn(&client->dev,
  745. "Unsupported ohm value %u in dt\n", ohms);
  746. return 0;
  747. }
  748. return setup;
  749. }
  750. static void ds1307_trickle_of_init(struct i2c_client *client,
  751. struct chip_desc *chip)
  752. {
  753. uint32_t ohms = 0;
  754. bool diode = true;
  755. if (!chip->do_trickle_setup)
  756. goto out;
  757. if (of_property_read_u32(client->dev.of_node, "trickle-resistor-ohms" , &ohms))
  758. goto out;
  759. if (of_property_read_bool(client->dev.of_node, "trickle-diode-disable"))
  760. diode = false;
  761. chip->trickle_charger_setup = chip->do_trickle_setup(client,
  762. ohms, diode);
  763. out:
  764. return;
  765. }
  766. /*----------------------------------------------------------------------*/
  767. #ifdef CONFIG_RTC_DRV_DS1307_HWMON
  768. /*
  769. * Temperature sensor support for ds3231 devices.
  770. */
  771. #define DS3231_REG_TEMPERATURE 0x11
  772. /*
  773. * A user-initiated temperature conversion is not started by this function,
  774. * so the temperature is updated once every 64 seconds.
  775. */
  776. static int ds3231_hwmon_read_temp(struct device *dev, s32 *mC)
  777. {
  778. struct ds1307 *ds1307 = dev_get_drvdata(dev);
  779. u8 temp_buf[2];
  780. s16 temp;
  781. int ret;
  782. ret = ds1307->read_block_data(ds1307->client, DS3231_REG_TEMPERATURE,
  783. sizeof(temp_buf), temp_buf);
  784. if (ret < 0)
  785. return ret;
  786. if (ret != sizeof(temp_buf))
  787. return -EIO;
  788. /*
  789. * Temperature is represented as a 10-bit code with a resolution of
  790. * 0.25 degree celsius and encoded in two's complement format.
  791. */
  792. temp = (temp_buf[0] << 8) | temp_buf[1];
  793. temp >>= 6;
  794. *mC = temp * 250;
  795. return 0;
  796. }
  797. static ssize_t ds3231_hwmon_show_temp(struct device *dev,
  798. struct device_attribute *attr, char *buf)
  799. {
  800. int ret;
  801. s32 temp;
  802. ret = ds3231_hwmon_read_temp(dev, &temp);
  803. if (ret)
  804. return ret;
  805. return sprintf(buf, "%d\n", temp);
  806. }
  807. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, ds3231_hwmon_show_temp,
  808. NULL, 0);
  809. static struct attribute *ds3231_hwmon_attrs[] = {
  810. &sensor_dev_attr_temp1_input.dev_attr.attr,
  811. NULL,
  812. };
  813. ATTRIBUTE_GROUPS(ds3231_hwmon);
  814. static void ds1307_hwmon_register(struct ds1307 *ds1307)
  815. {
  816. struct device *dev;
  817. if (ds1307->type != ds_3231)
  818. return;
  819. dev = devm_hwmon_device_register_with_groups(&ds1307->client->dev,
  820. ds1307->client->name,
  821. ds1307, ds3231_hwmon_groups);
  822. if (IS_ERR(dev)) {
  823. dev_warn(&ds1307->client->dev,
  824. "unable to register hwmon device %ld\n", PTR_ERR(dev));
  825. }
  826. }
  827. #else
  828. static void ds1307_hwmon_register(struct ds1307 *ds1307)
  829. {
  830. }
  831. #endif /* CONFIG_RTC_DRV_DS1307_HWMON */
  832. /*----------------------------------------------------------------------*/
  833. /*
  834. * Square-wave output support for DS3231
  835. * Datasheet: https://datasheets.maximintegrated.com/en/ds/DS3231.pdf
  836. */
  837. #ifdef CONFIG_COMMON_CLK
  838. enum {
  839. DS3231_CLK_SQW = 0,
  840. DS3231_CLK_32KHZ,
  841. };
  842. #define clk_sqw_to_ds1307(clk) \
  843. container_of(clk, struct ds1307, clks[DS3231_CLK_SQW])
  844. #define clk_32khz_to_ds1307(clk) \
  845. container_of(clk, struct ds1307, clks[DS3231_CLK_32KHZ])
  846. static int ds3231_clk_sqw_rates[] = {
  847. 1,
  848. 1024,
  849. 4096,
  850. 8192,
  851. };
  852. static int ds1337_write_control(struct ds1307 *ds1307, u8 mask, u8 value)
  853. {
  854. struct i2c_client *client = ds1307->client;
  855. struct mutex *lock = &ds1307->rtc->ops_lock;
  856. int control;
  857. int ret;
  858. mutex_lock(lock);
  859. control = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL);
  860. if (control < 0) {
  861. ret = control;
  862. goto out;
  863. }
  864. control &= ~mask;
  865. control |= value;
  866. ret = i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL, control);
  867. out:
  868. mutex_unlock(lock);
  869. return ret;
  870. }
  871. static unsigned long ds3231_clk_sqw_recalc_rate(struct clk_hw *hw,
  872. unsigned long parent_rate)
  873. {
  874. struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
  875. int control;
  876. int rate_sel = 0;
  877. control = i2c_smbus_read_byte_data(ds1307->client, DS1337_REG_CONTROL);
  878. if (control < 0)
  879. return control;
  880. if (control & DS1337_BIT_RS1)
  881. rate_sel += 1;
  882. if (control & DS1337_BIT_RS2)
  883. rate_sel += 2;
  884. return ds3231_clk_sqw_rates[rate_sel];
  885. }
  886. static long ds3231_clk_sqw_round_rate(struct clk_hw *hw, unsigned long rate,
  887. unsigned long *prate)
  888. {
  889. int i;
  890. for (i = ARRAY_SIZE(ds3231_clk_sqw_rates) - 1; i >= 0; i--) {
  891. if (ds3231_clk_sqw_rates[i] <= rate)
  892. return ds3231_clk_sqw_rates[i];
  893. }
  894. return 0;
  895. }
  896. static int ds3231_clk_sqw_set_rate(struct clk_hw *hw, unsigned long rate,
  897. unsigned long parent_rate)
  898. {
  899. struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
  900. int control = 0;
  901. int rate_sel;
  902. for (rate_sel = 0; rate_sel < ARRAY_SIZE(ds3231_clk_sqw_rates);
  903. rate_sel++) {
  904. if (ds3231_clk_sqw_rates[rate_sel] == rate)
  905. break;
  906. }
  907. if (rate_sel == ARRAY_SIZE(ds3231_clk_sqw_rates))
  908. return -EINVAL;
  909. if (rate_sel & 1)
  910. control |= DS1337_BIT_RS1;
  911. if (rate_sel & 2)
  912. control |= DS1337_BIT_RS2;
  913. return ds1337_write_control(ds1307, DS1337_BIT_RS1 | DS1337_BIT_RS2,
  914. control);
  915. }
  916. static int ds3231_clk_sqw_prepare(struct clk_hw *hw)
  917. {
  918. struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
  919. return ds1337_write_control(ds1307, DS1337_BIT_INTCN, 0);
  920. }
  921. static void ds3231_clk_sqw_unprepare(struct clk_hw *hw)
  922. {
  923. struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
  924. ds1337_write_control(ds1307, DS1337_BIT_INTCN, DS1337_BIT_INTCN);
  925. }
  926. static int ds3231_clk_sqw_is_prepared(struct clk_hw *hw)
  927. {
  928. struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
  929. int control;
  930. control = i2c_smbus_read_byte_data(ds1307->client, DS1337_REG_CONTROL);
  931. if (control < 0)
  932. return control;
  933. return !(control & DS1337_BIT_INTCN);
  934. }
  935. static const struct clk_ops ds3231_clk_sqw_ops = {
  936. .prepare = ds3231_clk_sqw_prepare,
  937. .unprepare = ds3231_clk_sqw_unprepare,
  938. .is_prepared = ds3231_clk_sqw_is_prepared,
  939. .recalc_rate = ds3231_clk_sqw_recalc_rate,
  940. .round_rate = ds3231_clk_sqw_round_rate,
  941. .set_rate = ds3231_clk_sqw_set_rate,
  942. };
  943. static unsigned long ds3231_clk_32khz_recalc_rate(struct clk_hw *hw,
  944. unsigned long parent_rate)
  945. {
  946. return 32768;
  947. }
  948. static int ds3231_clk_32khz_control(struct ds1307 *ds1307, bool enable)
  949. {
  950. struct i2c_client *client = ds1307->client;
  951. struct mutex *lock = &ds1307->rtc->ops_lock;
  952. int status;
  953. int ret;
  954. mutex_lock(lock);
  955. status = i2c_smbus_read_byte_data(client, DS1337_REG_STATUS);
  956. if (status < 0) {
  957. ret = status;
  958. goto out;
  959. }
  960. if (enable)
  961. status |= DS3231_BIT_EN32KHZ;
  962. else
  963. status &= ~DS3231_BIT_EN32KHZ;
  964. ret = i2c_smbus_write_byte_data(client, DS1337_REG_STATUS, status);
  965. out:
  966. mutex_unlock(lock);
  967. return ret;
  968. }
  969. static int ds3231_clk_32khz_prepare(struct clk_hw *hw)
  970. {
  971. struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
  972. return ds3231_clk_32khz_control(ds1307, true);
  973. }
  974. static void ds3231_clk_32khz_unprepare(struct clk_hw *hw)
  975. {
  976. struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
  977. ds3231_clk_32khz_control(ds1307, false);
  978. }
  979. static int ds3231_clk_32khz_is_prepared(struct clk_hw *hw)
  980. {
  981. struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
  982. int status;
  983. status = i2c_smbus_read_byte_data(ds1307->client, DS1337_REG_STATUS);
  984. if (status < 0)
  985. return status;
  986. return !!(status & DS3231_BIT_EN32KHZ);
  987. }
  988. static const struct clk_ops ds3231_clk_32khz_ops = {
  989. .prepare = ds3231_clk_32khz_prepare,
  990. .unprepare = ds3231_clk_32khz_unprepare,
  991. .is_prepared = ds3231_clk_32khz_is_prepared,
  992. .recalc_rate = ds3231_clk_32khz_recalc_rate,
  993. };
  994. static struct clk_init_data ds3231_clks_init[] = {
  995. [DS3231_CLK_SQW] = {
  996. .name = "ds3231_clk_sqw",
  997. .ops = &ds3231_clk_sqw_ops,
  998. },
  999. [DS3231_CLK_32KHZ] = {
  1000. .name = "ds3231_clk_32khz",
  1001. .ops = &ds3231_clk_32khz_ops,
  1002. },
  1003. };
  1004. static int ds3231_clks_register(struct ds1307 *ds1307)
  1005. {
  1006. struct i2c_client *client = ds1307->client;
  1007. struct device_node *node = client->dev.of_node;
  1008. struct clk_onecell_data *onecell;
  1009. int i;
  1010. onecell = devm_kzalloc(&client->dev, sizeof(*onecell), GFP_KERNEL);
  1011. if (!onecell)
  1012. return -ENOMEM;
  1013. onecell->clk_num = ARRAY_SIZE(ds3231_clks_init);
  1014. onecell->clks = devm_kcalloc(&client->dev, onecell->clk_num,
  1015. sizeof(onecell->clks[0]), GFP_KERNEL);
  1016. if (!onecell->clks)
  1017. return -ENOMEM;
  1018. for (i = 0; i < ARRAY_SIZE(ds3231_clks_init); i++) {
  1019. struct clk_init_data init = ds3231_clks_init[i];
  1020. /*
  1021. * Interrupt signal due to alarm conditions and square-wave
  1022. * output share same pin, so don't initialize both.
  1023. */
  1024. if (i == DS3231_CLK_SQW && test_bit(HAS_ALARM, &ds1307->flags))
  1025. continue;
  1026. /* optional override of the clockname */
  1027. of_property_read_string_index(node, "clock-output-names", i,
  1028. &init.name);
  1029. ds1307->clks[i].init = &init;
  1030. onecell->clks[i] = devm_clk_register(&client->dev,
  1031. &ds1307->clks[i]);
  1032. if (IS_ERR(onecell->clks[i]))
  1033. return PTR_ERR(onecell->clks[i]);
  1034. }
  1035. if (!node)
  1036. return 0;
  1037. of_clk_add_provider(node, of_clk_src_onecell_get, onecell);
  1038. return 0;
  1039. }
  1040. static void ds1307_clks_register(struct ds1307 *ds1307)
  1041. {
  1042. int ret;
  1043. if (ds1307->type != ds_3231)
  1044. return;
  1045. ret = ds3231_clks_register(ds1307);
  1046. if (ret) {
  1047. dev_warn(&ds1307->client->dev,
  1048. "unable to register clock device %d\n", ret);
  1049. }
  1050. }
  1051. #else
  1052. static void ds1307_clks_register(struct ds1307 *ds1307)
  1053. {
  1054. }
  1055. #endif /* CONFIG_COMMON_CLK */
  1056. static int ds1307_probe(struct i2c_client *client,
  1057. const struct i2c_device_id *id)
  1058. {
  1059. struct ds1307 *ds1307;
  1060. int err = -ENODEV;
  1061. int tmp, wday;
  1062. struct chip_desc *chip = &chips[id->driver_data];
  1063. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  1064. bool want_irq = false;
  1065. bool ds1307_can_wakeup_device = false;
  1066. unsigned char *buf;
  1067. struct ds1307_platform_data *pdata = dev_get_platdata(&client->dev);
  1068. struct rtc_time tm;
  1069. unsigned long timestamp;
  1070. irq_handler_t irq_handler = ds1307_irq;
  1071. static const int bbsqi_bitpos[] = {
  1072. [ds_1337] = 0,
  1073. [ds_1339] = DS1339_BIT_BBSQI,
  1074. [ds_3231] = DS3231_BIT_BBSQW,
  1075. };
  1076. const struct rtc_class_ops *rtc_ops = &ds13xx_rtc_ops;
  1077. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)
  1078. && !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
  1079. return -EIO;
  1080. ds1307 = devm_kzalloc(&client->dev, sizeof(struct ds1307), GFP_KERNEL);
  1081. if (!ds1307)
  1082. return -ENOMEM;
  1083. i2c_set_clientdata(client, ds1307);
  1084. ds1307->client = client;
  1085. ds1307->type = id->driver_data;
  1086. if (!pdata && client->dev.of_node)
  1087. ds1307_trickle_of_init(client, chip);
  1088. else if (pdata && pdata->trickle_charger_setup)
  1089. chip->trickle_charger_setup = pdata->trickle_charger_setup;
  1090. if (chip->trickle_charger_setup && chip->trickle_charger_reg) {
  1091. dev_dbg(&client->dev, "writing trickle charger info 0x%x to 0x%x\n",
  1092. DS13XX_TRICKLE_CHARGER_MAGIC | chip->trickle_charger_setup,
  1093. chip->trickle_charger_reg);
  1094. i2c_smbus_write_byte_data(client, chip->trickle_charger_reg,
  1095. DS13XX_TRICKLE_CHARGER_MAGIC |
  1096. chip->trickle_charger_setup);
  1097. }
  1098. buf = ds1307->regs;
  1099. if (i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) {
  1100. ds1307->read_block_data = ds1307_native_smbus_read_block_data;
  1101. ds1307->write_block_data = ds1307_native_smbus_write_block_data;
  1102. } else {
  1103. ds1307->read_block_data = ds1307_read_block_data;
  1104. ds1307->write_block_data = ds1307_write_block_data;
  1105. }
  1106. #ifdef CONFIG_OF
  1107. /*
  1108. * For devices with no IRQ directly connected to the SoC, the RTC chip
  1109. * can be forced as a wakeup source by stating that explicitly in
  1110. * the device's .dts file using the "wakeup-source" boolean property.
  1111. * If the "wakeup-source" property is set, don't request an IRQ.
  1112. * This will guarantee the 'wakealarm' sysfs entry is available on the device,
  1113. * if supported by the RTC.
  1114. */
  1115. if (of_property_read_bool(client->dev.of_node, "wakeup-source")) {
  1116. ds1307_can_wakeup_device = true;
  1117. }
  1118. /* Intersil ISL12057 DT backward compatibility */
  1119. if (of_property_read_bool(client->dev.of_node,
  1120. "isil,irq2-can-wakeup-machine")) {
  1121. ds1307_can_wakeup_device = true;
  1122. }
  1123. #endif
  1124. switch (ds1307->type) {
  1125. case ds_1337:
  1126. case ds_1339:
  1127. case ds_3231:
  1128. /* get registers that the "rtc" read below won't read... */
  1129. tmp = ds1307->read_block_data(ds1307->client,
  1130. DS1337_REG_CONTROL, 2, buf);
  1131. if (tmp != 2) {
  1132. dev_dbg(&client->dev, "read error %d\n", tmp);
  1133. err = -EIO;
  1134. goto exit;
  1135. }
  1136. /* oscillator off? turn it on, so clock can tick. */
  1137. if (ds1307->regs[0] & DS1337_BIT_nEOSC)
  1138. ds1307->regs[0] &= ~DS1337_BIT_nEOSC;
  1139. /*
  1140. * Using IRQ or defined as wakeup-source?
  1141. * Disable the square wave and both alarms.
  1142. * For some variants, be sure alarms can trigger when we're
  1143. * running on Vbackup (BBSQI/BBSQW)
  1144. */
  1145. if (chip->alarm && (ds1307->client->irq > 0 ||
  1146. ds1307_can_wakeup_device)) {
  1147. ds1307->regs[0] |= DS1337_BIT_INTCN
  1148. | bbsqi_bitpos[ds1307->type];
  1149. ds1307->regs[0] &= ~(DS1337_BIT_A2IE | DS1337_BIT_A1IE);
  1150. want_irq = true;
  1151. }
  1152. i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL,
  1153. ds1307->regs[0]);
  1154. /* oscillator fault? clear flag, and warn */
  1155. if (ds1307->regs[1] & DS1337_BIT_OSF) {
  1156. i2c_smbus_write_byte_data(client, DS1337_REG_STATUS,
  1157. ds1307->regs[1] & ~DS1337_BIT_OSF);
  1158. dev_warn(&client->dev, "SET TIME!\n");
  1159. }
  1160. break;
  1161. case rx_8025:
  1162. tmp = i2c_smbus_read_i2c_block_data(ds1307->client,
  1163. RX8025_REG_CTRL1 << 4 | 0x08, 2, buf);
  1164. if (tmp != 2) {
  1165. dev_dbg(&client->dev, "read error %d\n", tmp);
  1166. err = -EIO;
  1167. goto exit;
  1168. }
  1169. /* oscillator off? turn it on, so clock can tick. */
  1170. if (!(ds1307->regs[1] & RX8025_BIT_XST)) {
  1171. ds1307->regs[1] |= RX8025_BIT_XST;
  1172. i2c_smbus_write_byte_data(client,
  1173. RX8025_REG_CTRL2 << 4 | 0x08,
  1174. ds1307->regs[1]);
  1175. dev_warn(&client->dev,
  1176. "oscillator stop detected - SET TIME!\n");
  1177. }
  1178. if (ds1307->regs[1] & RX8025_BIT_PON) {
  1179. ds1307->regs[1] &= ~RX8025_BIT_PON;
  1180. i2c_smbus_write_byte_data(client,
  1181. RX8025_REG_CTRL2 << 4 | 0x08,
  1182. ds1307->regs[1]);
  1183. dev_warn(&client->dev, "power-on detected\n");
  1184. }
  1185. if (ds1307->regs[1] & RX8025_BIT_VDET) {
  1186. ds1307->regs[1] &= ~RX8025_BIT_VDET;
  1187. i2c_smbus_write_byte_data(client,
  1188. RX8025_REG_CTRL2 << 4 | 0x08,
  1189. ds1307->regs[1]);
  1190. dev_warn(&client->dev, "voltage drop detected\n");
  1191. }
  1192. /* make sure we are running in 24hour mode */
  1193. if (!(ds1307->regs[0] & RX8025_BIT_2412)) {
  1194. u8 hour;
  1195. /* switch to 24 hour mode */
  1196. i2c_smbus_write_byte_data(client,
  1197. RX8025_REG_CTRL1 << 4 | 0x08,
  1198. ds1307->regs[0] |
  1199. RX8025_BIT_2412);
  1200. tmp = i2c_smbus_read_i2c_block_data(ds1307->client,
  1201. RX8025_REG_CTRL1 << 4 | 0x08, 2, buf);
  1202. if (tmp != 2) {
  1203. dev_dbg(&client->dev, "read error %d\n", tmp);
  1204. err = -EIO;
  1205. goto exit;
  1206. }
  1207. /* correct hour */
  1208. hour = bcd2bin(ds1307->regs[DS1307_REG_HOUR]);
  1209. if (hour == 12)
  1210. hour = 0;
  1211. if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
  1212. hour += 12;
  1213. i2c_smbus_write_byte_data(client,
  1214. DS1307_REG_HOUR << 4 | 0x08,
  1215. hour);
  1216. }
  1217. break;
  1218. case ds_1388:
  1219. ds1307->offset = 1; /* Seconds starts at 1 */
  1220. break;
  1221. case mcp794xx:
  1222. rtc_ops = &mcp794xx_rtc_ops;
  1223. if (ds1307->client->irq > 0 && chip->alarm) {
  1224. irq_handler = mcp794xx_irq;
  1225. want_irq = true;
  1226. }
  1227. break;
  1228. default:
  1229. break;
  1230. }
  1231. read_rtc:
  1232. /* read RTC registers */
  1233. tmp = ds1307->read_block_data(ds1307->client, ds1307->offset, 8, buf);
  1234. if (tmp != 8) {
  1235. dev_dbg(&client->dev, "read error %d\n", tmp);
  1236. err = -EIO;
  1237. goto exit;
  1238. }
  1239. /*
  1240. * minimal sanity checking; some chips (like DS1340) don't
  1241. * specify the extra bits as must-be-zero, but there are
  1242. * still a few values that are clearly out-of-range.
  1243. */
  1244. tmp = ds1307->regs[DS1307_REG_SECS];
  1245. switch (ds1307->type) {
  1246. case ds_1307:
  1247. case m41t00:
  1248. /* clock halted? turn it on, so clock can tick. */
  1249. if (tmp & DS1307_BIT_CH) {
  1250. i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
  1251. dev_warn(&client->dev, "SET TIME!\n");
  1252. goto read_rtc;
  1253. }
  1254. break;
  1255. case ds_1338:
  1256. /* clock halted? turn it on, so clock can tick. */
  1257. if (tmp & DS1307_BIT_CH)
  1258. i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
  1259. /* oscillator fault? clear flag, and warn */
  1260. if (ds1307->regs[DS1307_REG_CONTROL] & DS1338_BIT_OSF) {
  1261. i2c_smbus_write_byte_data(client, DS1307_REG_CONTROL,
  1262. ds1307->regs[DS1307_REG_CONTROL]
  1263. & ~DS1338_BIT_OSF);
  1264. dev_warn(&client->dev, "SET TIME!\n");
  1265. goto read_rtc;
  1266. }
  1267. break;
  1268. case ds_1340:
  1269. /* clock halted? turn it on, so clock can tick. */
  1270. if (tmp & DS1340_BIT_nEOSC)
  1271. i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
  1272. tmp = i2c_smbus_read_byte_data(client, DS1340_REG_FLAG);
  1273. if (tmp < 0) {
  1274. dev_dbg(&client->dev, "read error %d\n", tmp);
  1275. err = -EIO;
  1276. goto exit;
  1277. }
  1278. /* oscillator fault? clear flag, and warn */
  1279. if (tmp & DS1340_BIT_OSF) {
  1280. i2c_smbus_write_byte_data(client, DS1340_REG_FLAG, 0);
  1281. dev_warn(&client->dev, "SET TIME!\n");
  1282. }
  1283. break;
  1284. case mcp794xx:
  1285. /* make sure that the backup battery is enabled */
  1286. if (!(ds1307->regs[DS1307_REG_WDAY] & MCP794XX_BIT_VBATEN)) {
  1287. i2c_smbus_write_byte_data(client, DS1307_REG_WDAY,
  1288. ds1307->regs[DS1307_REG_WDAY]
  1289. | MCP794XX_BIT_VBATEN);
  1290. }
  1291. /* clock halted? turn it on, so clock can tick. */
  1292. if (!(tmp & MCP794XX_BIT_ST)) {
  1293. i2c_smbus_write_byte_data(client, DS1307_REG_SECS,
  1294. MCP794XX_BIT_ST);
  1295. dev_warn(&client->dev, "SET TIME!\n");
  1296. goto read_rtc;
  1297. }
  1298. break;
  1299. default:
  1300. break;
  1301. }
  1302. tmp = ds1307->regs[DS1307_REG_HOUR];
  1303. switch (ds1307->type) {
  1304. case ds_1340:
  1305. case m41t00:
  1306. /*
  1307. * NOTE: ignores century bits; fix before deploying
  1308. * systems that will run through year 2100.
  1309. */
  1310. break;
  1311. case rx_8025:
  1312. break;
  1313. default:
  1314. if (!(tmp & DS1307_BIT_12HR))
  1315. break;
  1316. /*
  1317. * Be sure we're in 24 hour mode. Multi-master systems
  1318. * take note...
  1319. */
  1320. tmp = bcd2bin(tmp & 0x1f);
  1321. if (tmp == 12)
  1322. tmp = 0;
  1323. if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
  1324. tmp += 12;
  1325. i2c_smbus_write_byte_data(client,
  1326. ds1307->offset + DS1307_REG_HOUR,
  1327. bin2bcd(tmp));
  1328. }
  1329. /*
  1330. * Some IPs have weekday reset value = 0x1 which might not correct
  1331. * hence compute the wday using the current date/month/year values
  1332. */
  1333. ds1307_get_time(&client->dev, &tm);
  1334. wday = tm.tm_wday;
  1335. timestamp = rtc_tm_to_time64(&tm);
  1336. rtc_time64_to_tm(timestamp, &tm);
  1337. /*
  1338. * Check if reset wday is different from the computed wday
  1339. * If different then set the wday which we computed using
  1340. * timestamp
  1341. */
  1342. if (wday != tm.tm_wday) {
  1343. wday = i2c_smbus_read_byte_data(client, MCP794XX_REG_WEEKDAY);
  1344. wday = wday & ~MCP794XX_REG_WEEKDAY_WDAY_MASK;
  1345. wday = wday | (tm.tm_wday + 1);
  1346. i2c_smbus_write_byte_data(client, MCP794XX_REG_WEEKDAY, wday);
  1347. }
  1348. if (want_irq) {
  1349. device_set_wakeup_capable(&client->dev, true);
  1350. set_bit(HAS_ALARM, &ds1307->flags);
  1351. }
  1352. ds1307->rtc = devm_rtc_device_register(&client->dev, client->name,
  1353. rtc_ops, THIS_MODULE);
  1354. if (IS_ERR(ds1307->rtc)) {
  1355. return PTR_ERR(ds1307->rtc);
  1356. }
  1357. if (ds1307_can_wakeup_device && ds1307->client->irq <= 0) {
  1358. /* Disable request for an IRQ */
  1359. want_irq = false;
  1360. dev_info(&client->dev, "'wakeup-source' is set, request for an IRQ is disabled!\n");
  1361. /* We cannot support UIE mode if we do not have an IRQ line */
  1362. ds1307->rtc->uie_unsupported = 1;
  1363. }
  1364. if (want_irq) {
  1365. err = devm_request_threaded_irq(&client->dev,
  1366. client->irq, NULL, irq_handler,
  1367. IRQF_SHARED | IRQF_ONESHOT,
  1368. ds1307->rtc->name, client);
  1369. if (err) {
  1370. client->irq = 0;
  1371. device_set_wakeup_capable(&client->dev, false);
  1372. clear_bit(HAS_ALARM, &ds1307->flags);
  1373. dev_err(&client->dev, "unable to request IRQ!\n");
  1374. } else
  1375. dev_dbg(&client->dev, "got IRQ %d\n", client->irq);
  1376. }
  1377. if (chip->nvram_size) {
  1378. ds1307->nvram = devm_kzalloc(&client->dev,
  1379. sizeof(struct bin_attribute),
  1380. GFP_KERNEL);
  1381. if (!ds1307->nvram) {
  1382. dev_err(&client->dev, "cannot allocate memory for nvram sysfs\n");
  1383. } else {
  1384. ds1307->nvram->attr.name = "nvram";
  1385. ds1307->nvram->attr.mode = S_IRUGO | S_IWUSR;
  1386. sysfs_bin_attr_init(ds1307->nvram);
  1387. ds1307->nvram->read = ds1307_nvram_read;
  1388. ds1307->nvram->write = ds1307_nvram_write;
  1389. ds1307->nvram->size = chip->nvram_size;
  1390. ds1307->nvram_offset = chip->nvram_offset;
  1391. err = sysfs_create_bin_file(&client->dev.kobj,
  1392. ds1307->nvram);
  1393. if (err) {
  1394. dev_err(&client->dev,
  1395. "unable to create sysfs file: %s\n",
  1396. ds1307->nvram->attr.name);
  1397. } else {
  1398. set_bit(HAS_NVRAM, &ds1307->flags);
  1399. dev_info(&client->dev, "%zu bytes nvram\n",
  1400. ds1307->nvram->size);
  1401. }
  1402. }
  1403. }
  1404. ds1307_hwmon_register(ds1307);
  1405. ds1307_clks_register(ds1307);
  1406. return 0;
  1407. exit:
  1408. return err;
  1409. }
  1410. static int ds1307_remove(struct i2c_client *client)
  1411. {
  1412. struct ds1307 *ds1307 = i2c_get_clientdata(client);
  1413. if (test_and_clear_bit(HAS_NVRAM, &ds1307->flags))
  1414. sysfs_remove_bin_file(&client->dev.kobj, ds1307->nvram);
  1415. return 0;
  1416. }
  1417. static struct i2c_driver ds1307_driver = {
  1418. .driver = {
  1419. .name = "rtc-ds1307",
  1420. },
  1421. .probe = ds1307_probe,
  1422. .remove = ds1307_remove,
  1423. .id_table = ds1307_id,
  1424. };
  1425. module_i2c_driver(ds1307_driver);
  1426. MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips");
  1427. MODULE_LICENSE("GPL");