rtc-ds1307.c 46 KB

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