rtc-ds1307.c 44 KB

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