rtc-ds1307.c 45 KB

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