rtc-ds1307.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259
  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/module.h>
  14. #include <linux/init.h>
  15. #include <linux/slab.h>
  16. #include <linux/i2c.h>
  17. #include <linux/string.h>
  18. #include <linux/rtc.h>
  19. #include <linux/bcd.h>
  20. #include <linux/rtc/ds1307.h>
  21. /*
  22. * We can't determine type by probing, but if we expect pre-Linux code
  23. * to have set the chip up as a clock (turning on the oscillator and
  24. * setting the date and time), Linux can ignore the non-clock features.
  25. * That's a natural job for a factory or repair bench.
  26. */
  27. enum ds_type {
  28. ds_1307,
  29. ds_1337,
  30. ds_1338,
  31. ds_1339,
  32. ds_1340,
  33. ds_1388,
  34. ds_3231,
  35. m41t00,
  36. mcp794xx,
  37. rx_8025,
  38. last_ds_type /* always last */
  39. /* rs5c372 too? different address... */
  40. };
  41. /* RTC registers don't differ much, except for the century flag */
  42. #define DS1307_REG_SECS 0x00 /* 00-59 */
  43. # define DS1307_BIT_CH 0x80
  44. # define DS1340_BIT_nEOSC 0x80
  45. # define MCP794XX_BIT_ST 0x80
  46. #define DS1307_REG_MIN 0x01 /* 00-59 */
  47. #define DS1307_REG_HOUR 0x02 /* 00-23, or 1-12{am,pm} */
  48. # define DS1307_BIT_12HR 0x40 /* in REG_HOUR */
  49. # define DS1307_BIT_PM 0x20 /* in REG_HOUR */
  50. # define DS1340_BIT_CENTURY_EN 0x80 /* in REG_HOUR */
  51. # define DS1340_BIT_CENTURY 0x40 /* in REG_HOUR */
  52. #define DS1307_REG_WDAY 0x03 /* 01-07 */
  53. # define MCP794XX_BIT_VBATEN 0x08
  54. #define DS1307_REG_MDAY 0x04 /* 01-31 */
  55. #define DS1307_REG_MONTH 0x05 /* 01-12 */
  56. # define DS1337_BIT_CENTURY 0x80 /* in REG_MONTH */
  57. #define DS1307_REG_YEAR 0x06 /* 00-99 */
  58. /*
  59. * Other registers (control, status, alarms, trickle charge, NVRAM, etc)
  60. * start at 7, and they differ a LOT. Only control and status matter for
  61. * basic RTC date and time functionality; be careful using them.
  62. */
  63. #define DS1307_REG_CONTROL 0x07 /* or ds1338 */
  64. # define DS1307_BIT_OUT 0x80
  65. # define DS1338_BIT_OSF 0x20
  66. # define DS1307_BIT_SQWE 0x10
  67. # define DS1307_BIT_RS1 0x02
  68. # define DS1307_BIT_RS0 0x01
  69. #define DS1337_REG_CONTROL 0x0e
  70. # define DS1337_BIT_nEOSC 0x80
  71. # define DS1339_BIT_BBSQI 0x20
  72. # define DS3231_BIT_BBSQW 0x40 /* same as BBSQI */
  73. # define DS1337_BIT_RS2 0x10
  74. # define DS1337_BIT_RS1 0x08
  75. # define DS1337_BIT_INTCN 0x04
  76. # define DS1337_BIT_A2IE 0x02
  77. # define DS1337_BIT_A1IE 0x01
  78. #define DS1340_REG_CONTROL 0x07
  79. # define DS1340_BIT_OUT 0x80
  80. # define DS1340_BIT_FT 0x40
  81. # define DS1340_BIT_CALIB_SIGN 0x20
  82. # define DS1340_M_CALIBRATION 0x1f
  83. #define DS1340_REG_FLAG 0x09
  84. # define DS1340_BIT_OSF 0x80
  85. #define DS1337_REG_STATUS 0x0f
  86. # define DS1337_BIT_OSF 0x80
  87. # define DS1337_BIT_A2I 0x02
  88. # define DS1337_BIT_A1I 0x01
  89. #define DS1339_REG_ALARM1_SECS 0x07
  90. #define DS13XX_TRICKLE_CHARGER_MAGIC 0xa0
  91. #define RX8025_REG_CTRL1 0x0e
  92. # define RX8025_BIT_2412 0x20
  93. #define RX8025_REG_CTRL2 0x0f
  94. # define RX8025_BIT_PON 0x10
  95. # define RX8025_BIT_VDET 0x40
  96. # define RX8025_BIT_XST 0x20
  97. struct ds1307 {
  98. u8 offset; /* register's offset */
  99. u8 regs[11];
  100. u16 nvram_offset;
  101. struct bin_attribute *nvram;
  102. enum ds_type type;
  103. unsigned long flags;
  104. #define HAS_NVRAM 0 /* bit 0 == sysfs file active */
  105. #define HAS_ALARM 1 /* bit 1 == irq claimed */
  106. struct i2c_client *client;
  107. struct rtc_device *rtc;
  108. struct work_struct work;
  109. s32 (*read_block_data)(const struct i2c_client *client, u8 command,
  110. u8 length, u8 *values);
  111. s32 (*write_block_data)(const struct i2c_client *client, u8 command,
  112. u8 length, const u8 *values);
  113. };
  114. struct chip_desc {
  115. unsigned alarm:1;
  116. u16 nvram_offset;
  117. u16 nvram_size;
  118. u16 trickle_charger_reg;
  119. u8 trickle_charger_setup;
  120. u8 (*do_trickle_setup)(struct i2c_client *, uint32_t, bool);
  121. };
  122. static u8 do_trickle_setup_ds1339(struct i2c_client *,
  123. uint32_t ohms, bool diode);
  124. static struct chip_desc chips[last_ds_type] = {
  125. [ds_1307] = {
  126. .nvram_offset = 8,
  127. .nvram_size = 56,
  128. },
  129. [ds_1337] = {
  130. .alarm = 1,
  131. },
  132. [ds_1338] = {
  133. .nvram_offset = 8,
  134. .nvram_size = 56,
  135. },
  136. [ds_1339] = {
  137. .alarm = 1,
  138. .trickle_charger_reg = 0x10,
  139. .do_trickle_setup = &do_trickle_setup_ds1339,
  140. },
  141. [ds_1340] = {
  142. .trickle_charger_reg = 0x08,
  143. },
  144. [ds_1388] = {
  145. .trickle_charger_reg = 0x0a,
  146. },
  147. [ds_3231] = {
  148. .alarm = 1,
  149. },
  150. [mcp794xx] = {
  151. .alarm = 1,
  152. /* this is battery backed SRAM */
  153. .nvram_offset = 0x20,
  154. .nvram_size = 0x40,
  155. },
  156. };
  157. static const struct i2c_device_id ds1307_id[] = {
  158. { "ds1307", ds_1307 },
  159. { "ds1337", ds_1337 },
  160. { "ds1338", ds_1338 },
  161. { "ds1339", ds_1339 },
  162. { "ds1388", ds_1388 },
  163. { "ds1340", ds_1340 },
  164. { "ds3231", ds_3231 },
  165. { "m41t00", m41t00 },
  166. { "mcp7940x", mcp794xx },
  167. { "mcp7941x", mcp794xx },
  168. { "pt7c4338", ds_1307 },
  169. { "rx8025", rx_8025 },
  170. { }
  171. };
  172. MODULE_DEVICE_TABLE(i2c, ds1307_id);
  173. /*----------------------------------------------------------------------*/
  174. #define BLOCK_DATA_MAX_TRIES 10
  175. static s32 ds1307_read_block_data_once(const struct i2c_client *client,
  176. u8 command, u8 length, u8 *values)
  177. {
  178. s32 i, data;
  179. for (i = 0; i < length; i++) {
  180. data = i2c_smbus_read_byte_data(client, command + i);
  181. if (data < 0)
  182. return data;
  183. values[i] = data;
  184. }
  185. return i;
  186. }
  187. static s32 ds1307_read_block_data(const struct i2c_client *client, u8 command,
  188. u8 length, u8 *values)
  189. {
  190. u8 oldvalues[255];
  191. s32 ret;
  192. int tries = 0;
  193. dev_dbg(&client->dev, "ds1307_read_block_data (length=%d)\n", length);
  194. ret = ds1307_read_block_data_once(client, command, length, values);
  195. if (ret < 0)
  196. return ret;
  197. do {
  198. if (++tries > BLOCK_DATA_MAX_TRIES) {
  199. dev_err(&client->dev,
  200. "ds1307_read_block_data failed\n");
  201. return -EIO;
  202. }
  203. memcpy(oldvalues, values, length);
  204. ret = ds1307_read_block_data_once(client, command, length,
  205. values);
  206. if (ret < 0)
  207. return ret;
  208. } while (memcmp(oldvalues, values, length));
  209. return length;
  210. }
  211. static s32 ds1307_write_block_data(const struct i2c_client *client, u8 command,
  212. u8 length, const u8 *values)
  213. {
  214. u8 currvalues[255];
  215. int tries = 0;
  216. dev_dbg(&client->dev, "ds1307_write_block_data (length=%d)\n", length);
  217. do {
  218. s32 i, ret;
  219. if (++tries > BLOCK_DATA_MAX_TRIES) {
  220. dev_err(&client->dev,
  221. "ds1307_write_block_data failed\n");
  222. return -EIO;
  223. }
  224. for (i = 0; i < length; i++) {
  225. ret = i2c_smbus_write_byte_data(client, command + i,
  226. values[i]);
  227. if (ret < 0)
  228. return ret;
  229. }
  230. ret = ds1307_read_block_data_once(client, command, length,
  231. currvalues);
  232. if (ret < 0)
  233. return ret;
  234. } while (memcmp(currvalues, values, length));
  235. return length;
  236. }
  237. /*----------------------------------------------------------------------*/
  238. /* These RTC devices are not designed to be connected to a SMbus adapter.
  239. SMbus limits block operations length to 32 bytes, whereas it's not
  240. limited on I2C buses. As a result, accesses may exceed 32 bytes;
  241. in that case, split them into smaller blocks */
  242. static s32 ds1307_native_smbus_write_block_data(const struct i2c_client *client,
  243. u8 command, u8 length, const u8 *values)
  244. {
  245. u8 suboffset = 0;
  246. if (length <= I2C_SMBUS_BLOCK_MAX)
  247. return i2c_smbus_write_i2c_block_data(client,
  248. command, length, values);
  249. while (suboffset < length) {
  250. s32 retval = i2c_smbus_write_i2c_block_data(client,
  251. command + suboffset,
  252. min(I2C_SMBUS_BLOCK_MAX, length - suboffset),
  253. values + suboffset);
  254. if (retval < 0)
  255. return retval;
  256. suboffset += I2C_SMBUS_BLOCK_MAX;
  257. }
  258. return length;
  259. }
  260. static s32 ds1307_native_smbus_read_block_data(const struct i2c_client *client,
  261. u8 command, u8 length, u8 *values)
  262. {
  263. u8 suboffset = 0;
  264. if (length <= I2C_SMBUS_BLOCK_MAX)
  265. return i2c_smbus_read_i2c_block_data(client,
  266. command, length, values);
  267. while (suboffset < length) {
  268. s32 retval = i2c_smbus_read_i2c_block_data(client,
  269. command + suboffset,
  270. min(I2C_SMBUS_BLOCK_MAX, length - suboffset),
  271. values + suboffset);
  272. if (retval < 0)
  273. return retval;
  274. suboffset += I2C_SMBUS_BLOCK_MAX;
  275. }
  276. return length;
  277. }
  278. /*----------------------------------------------------------------------*/
  279. /*
  280. * The IRQ logic includes a "real" handler running in IRQ context just
  281. * long enough to schedule this workqueue entry. We need a task context
  282. * to talk to the RTC, since I2C I/O calls require that; and disable the
  283. * IRQ until we clear its status on the chip, so that this handler can
  284. * work with any type of triggering (not just falling edge).
  285. *
  286. * The ds1337 and ds1339 both have two alarms, but we only use the first
  287. * one (with a "seconds" field). For ds1337 we expect nINTA is our alarm
  288. * signal; ds1339 chips have only one alarm signal.
  289. */
  290. static void ds1307_work(struct work_struct *work)
  291. {
  292. struct ds1307 *ds1307;
  293. struct i2c_client *client;
  294. struct mutex *lock;
  295. int stat, control;
  296. ds1307 = container_of(work, struct ds1307, work);
  297. client = ds1307->client;
  298. lock = &ds1307->rtc->ops_lock;
  299. mutex_lock(lock);
  300. stat = i2c_smbus_read_byte_data(client, DS1337_REG_STATUS);
  301. if (stat < 0)
  302. goto out;
  303. if (stat & DS1337_BIT_A1I) {
  304. stat &= ~DS1337_BIT_A1I;
  305. i2c_smbus_write_byte_data(client, DS1337_REG_STATUS, stat);
  306. control = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL);
  307. if (control < 0)
  308. goto out;
  309. control &= ~DS1337_BIT_A1IE;
  310. i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL, control);
  311. rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
  312. }
  313. out:
  314. if (test_bit(HAS_ALARM, &ds1307->flags))
  315. enable_irq(client->irq);
  316. mutex_unlock(lock);
  317. }
  318. static irqreturn_t ds1307_irq(int irq, void *dev_id)
  319. {
  320. struct i2c_client *client = dev_id;
  321. struct ds1307 *ds1307 = i2c_get_clientdata(client);
  322. disable_irq_nosync(irq);
  323. schedule_work(&ds1307->work);
  324. return IRQ_HANDLED;
  325. }
  326. /*----------------------------------------------------------------------*/
  327. static int ds1307_get_time(struct device *dev, struct rtc_time *t)
  328. {
  329. struct ds1307 *ds1307 = dev_get_drvdata(dev);
  330. int tmp;
  331. /* read the RTC date and time registers all at once */
  332. tmp = ds1307->read_block_data(ds1307->client,
  333. ds1307->offset, 7, ds1307->regs);
  334. if (tmp != 7) {
  335. dev_err(dev, "%s error %d\n", "read", tmp);
  336. return -EIO;
  337. }
  338. dev_dbg(dev, "%s: %7ph\n", "read", ds1307->regs);
  339. t->tm_sec = bcd2bin(ds1307->regs[DS1307_REG_SECS] & 0x7f);
  340. t->tm_min = bcd2bin(ds1307->regs[DS1307_REG_MIN] & 0x7f);
  341. tmp = ds1307->regs[DS1307_REG_HOUR] & 0x3f;
  342. t->tm_hour = bcd2bin(tmp);
  343. t->tm_wday = bcd2bin(ds1307->regs[DS1307_REG_WDAY] & 0x07) - 1;
  344. t->tm_mday = bcd2bin(ds1307->regs[DS1307_REG_MDAY] & 0x3f);
  345. tmp = ds1307->regs[DS1307_REG_MONTH] & 0x1f;
  346. t->tm_mon = bcd2bin(tmp) - 1;
  347. /* assume 20YY not 19YY, and ignore DS1337_BIT_CENTURY */
  348. t->tm_year = bcd2bin(ds1307->regs[DS1307_REG_YEAR]) + 100;
  349. dev_dbg(dev, "%s secs=%d, mins=%d, "
  350. "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
  351. "read", t->tm_sec, t->tm_min,
  352. t->tm_hour, t->tm_mday,
  353. t->tm_mon, t->tm_year, t->tm_wday);
  354. /* initial clock setting can be undefined */
  355. return rtc_valid_tm(t);
  356. }
  357. static int ds1307_set_time(struct device *dev, struct rtc_time *t)
  358. {
  359. struct ds1307 *ds1307 = dev_get_drvdata(dev);
  360. int result;
  361. int tmp;
  362. u8 *buf = ds1307->regs;
  363. dev_dbg(dev, "%s secs=%d, mins=%d, "
  364. "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
  365. "write", t->tm_sec, t->tm_min,
  366. t->tm_hour, t->tm_mday,
  367. t->tm_mon, t->tm_year, t->tm_wday);
  368. buf[DS1307_REG_SECS] = bin2bcd(t->tm_sec);
  369. buf[DS1307_REG_MIN] = bin2bcd(t->tm_min);
  370. buf[DS1307_REG_HOUR] = bin2bcd(t->tm_hour);
  371. buf[DS1307_REG_WDAY] = bin2bcd(t->tm_wday + 1);
  372. buf[DS1307_REG_MDAY] = bin2bcd(t->tm_mday);
  373. buf[DS1307_REG_MONTH] = bin2bcd(t->tm_mon + 1);
  374. /* assume 20YY not 19YY */
  375. tmp = t->tm_year - 100;
  376. buf[DS1307_REG_YEAR] = bin2bcd(tmp);
  377. switch (ds1307->type) {
  378. case ds_1337:
  379. case ds_1339:
  380. case ds_3231:
  381. buf[DS1307_REG_MONTH] |= DS1337_BIT_CENTURY;
  382. break;
  383. case ds_1340:
  384. buf[DS1307_REG_HOUR] |= DS1340_BIT_CENTURY_EN
  385. | DS1340_BIT_CENTURY;
  386. break;
  387. case mcp794xx:
  388. /*
  389. * these bits were cleared when preparing the date/time
  390. * values and need to be set again before writing the
  391. * buffer out to the device.
  392. */
  393. buf[DS1307_REG_SECS] |= MCP794XX_BIT_ST;
  394. buf[DS1307_REG_WDAY] |= MCP794XX_BIT_VBATEN;
  395. break;
  396. default:
  397. break;
  398. }
  399. dev_dbg(dev, "%s: %7ph\n", "write", buf);
  400. result = ds1307->write_block_data(ds1307->client,
  401. ds1307->offset, 7, buf);
  402. if (result < 0) {
  403. dev_err(dev, "%s error %d\n", "write", result);
  404. return result;
  405. }
  406. return 0;
  407. }
  408. static int ds1337_read_alarm(struct device *dev, struct rtc_wkalrm *t)
  409. {
  410. struct i2c_client *client = to_i2c_client(dev);
  411. struct ds1307 *ds1307 = i2c_get_clientdata(client);
  412. int ret;
  413. if (!test_bit(HAS_ALARM, &ds1307->flags))
  414. return -EINVAL;
  415. /* read all ALARM1, ALARM2, and status registers at once */
  416. ret = ds1307->read_block_data(client,
  417. DS1339_REG_ALARM1_SECS, 9, ds1307->regs);
  418. if (ret != 9) {
  419. dev_err(dev, "%s error %d\n", "alarm read", ret);
  420. return -EIO;
  421. }
  422. dev_dbg(dev, "%s: %02x %02x %02x %02x, %02x %02x %02x, %02x %02x\n",
  423. "alarm read",
  424. ds1307->regs[0], ds1307->regs[1],
  425. ds1307->regs[2], ds1307->regs[3],
  426. ds1307->regs[4], ds1307->regs[5],
  427. ds1307->regs[6], ds1307->regs[7],
  428. ds1307->regs[8]);
  429. /*
  430. * report alarm time (ALARM1); assume 24 hour and day-of-month modes,
  431. * and that all four fields are checked matches
  432. */
  433. t->time.tm_sec = bcd2bin(ds1307->regs[0] & 0x7f);
  434. t->time.tm_min = bcd2bin(ds1307->regs[1] & 0x7f);
  435. t->time.tm_hour = bcd2bin(ds1307->regs[2] & 0x3f);
  436. t->time.tm_mday = bcd2bin(ds1307->regs[3] & 0x3f);
  437. t->time.tm_mon = -1;
  438. t->time.tm_year = -1;
  439. t->time.tm_wday = -1;
  440. t->time.tm_yday = -1;
  441. t->time.tm_isdst = -1;
  442. /* ... and status */
  443. t->enabled = !!(ds1307->regs[7] & DS1337_BIT_A1IE);
  444. t->pending = !!(ds1307->regs[8] & DS1337_BIT_A1I);
  445. dev_dbg(dev, "%s secs=%d, mins=%d, "
  446. "hours=%d, mday=%d, enabled=%d, pending=%d\n",
  447. "alarm read", t->time.tm_sec, t->time.tm_min,
  448. t->time.tm_hour, t->time.tm_mday,
  449. t->enabled, t->pending);
  450. return 0;
  451. }
  452. static int ds1337_set_alarm(struct device *dev, struct rtc_wkalrm *t)
  453. {
  454. struct i2c_client *client = to_i2c_client(dev);
  455. struct ds1307 *ds1307 = i2c_get_clientdata(client);
  456. unsigned char *buf = ds1307->regs;
  457. u8 control, status;
  458. int ret;
  459. if (!test_bit(HAS_ALARM, &ds1307->flags))
  460. return -EINVAL;
  461. dev_dbg(dev, "%s secs=%d, mins=%d, "
  462. "hours=%d, mday=%d, enabled=%d, pending=%d\n",
  463. "alarm set", t->time.tm_sec, t->time.tm_min,
  464. t->time.tm_hour, t->time.tm_mday,
  465. t->enabled, t->pending);
  466. /* read current status of both alarms and the chip */
  467. ret = ds1307->read_block_data(client,
  468. DS1339_REG_ALARM1_SECS, 9, buf);
  469. if (ret != 9) {
  470. dev_err(dev, "%s error %d\n", "alarm write", ret);
  471. return -EIO;
  472. }
  473. control = ds1307->regs[7];
  474. status = ds1307->regs[8];
  475. dev_dbg(dev, "%s: %02x %02x %02x %02x, %02x %02x %02x, %02x %02x\n",
  476. "alarm set (old status)",
  477. ds1307->regs[0], ds1307->regs[1],
  478. ds1307->regs[2], ds1307->regs[3],
  479. ds1307->regs[4], ds1307->regs[5],
  480. ds1307->regs[6], control, status);
  481. /* set ALARM1, using 24 hour and day-of-month modes */
  482. buf[0] = bin2bcd(t->time.tm_sec);
  483. buf[1] = bin2bcd(t->time.tm_min);
  484. buf[2] = bin2bcd(t->time.tm_hour);
  485. buf[3] = bin2bcd(t->time.tm_mday);
  486. /* set ALARM2 to non-garbage */
  487. buf[4] = 0;
  488. buf[5] = 0;
  489. buf[6] = 0;
  490. /* optionally enable ALARM1 */
  491. buf[7] = control & ~(DS1337_BIT_A1IE | DS1337_BIT_A2IE);
  492. if (t->enabled) {
  493. dev_dbg(dev, "alarm IRQ armed\n");
  494. buf[7] |= DS1337_BIT_A1IE; /* only ALARM1 is used */
  495. }
  496. buf[8] = status & ~(DS1337_BIT_A1I | DS1337_BIT_A2I);
  497. ret = ds1307->write_block_data(client,
  498. DS1339_REG_ALARM1_SECS, 9, buf);
  499. if (ret < 0) {
  500. dev_err(dev, "can't set alarm time\n");
  501. return ret;
  502. }
  503. return 0;
  504. }
  505. static int ds1307_alarm_irq_enable(struct device *dev, unsigned int enabled)
  506. {
  507. struct i2c_client *client = to_i2c_client(dev);
  508. struct ds1307 *ds1307 = i2c_get_clientdata(client);
  509. int ret;
  510. if (!test_bit(HAS_ALARM, &ds1307->flags))
  511. return -ENOTTY;
  512. ret = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL);
  513. if (ret < 0)
  514. return ret;
  515. if (enabled)
  516. ret |= DS1337_BIT_A1IE;
  517. else
  518. ret &= ~DS1337_BIT_A1IE;
  519. ret = i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL, ret);
  520. if (ret < 0)
  521. return ret;
  522. return 0;
  523. }
  524. static const struct rtc_class_ops ds13xx_rtc_ops = {
  525. .read_time = ds1307_get_time,
  526. .set_time = ds1307_set_time,
  527. .read_alarm = ds1337_read_alarm,
  528. .set_alarm = ds1337_set_alarm,
  529. .alarm_irq_enable = ds1307_alarm_irq_enable,
  530. };
  531. /*----------------------------------------------------------------------*/
  532. /*
  533. * Alarm support for mcp794xx devices.
  534. */
  535. #define MCP794XX_REG_CONTROL 0x07
  536. # define MCP794XX_BIT_ALM0_EN 0x10
  537. # define MCP794XX_BIT_ALM1_EN 0x20
  538. #define MCP794XX_REG_ALARM0_BASE 0x0a
  539. #define MCP794XX_REG_ALARM0_CTRL 0x0d
  540. #define MCP794XX_REG_ALARM1_BASE 0x11
  541. #define MCP794XX_REG_ALARM1_CTRL 0x14
  542. # define MCP794XX_BIT_ALMX_IF (1 << 3)
  543. # define MCP794XX_BIT_ALMX_C0 (1 << 4)
  544. # define MCP794XX_BIT_ALMX_C1 (1 << 5)
  545. # define MCP794XX_BIT_ALMX_C2 (1 << 6)
  546. # define MCP794XX_BIT_ALMX_POL (1 << 7)
  547. # define MCP794XX_MSK_ALMX_MATCH (MCP794XX_BIT_ALMX_C0 | \
  548. MCP794XX_BIT_ALMX_C1 | \
  549. MCP794XX_BIT_ALMX_C2)
  550. static void mcp794xx_work(struct work_struct *work)
  551. {
  552. struct ds1307 *ds1307 = container_of(work, struct ds1307, work);
  553. struct i2c_client *client = ds1307->client;
  554. int reg, ret;
  555. mutex_lock(&ds1307->rtc->ops_lock);
  556. /* Check and clear alarm 0 interrupt flag. */
  557. reg = i2c_smbus_read_byte_data(client, MCP794XX_REG_ALARM0_CTRL);
  558. if (reg < 0)
  559. goto out;
  560. if (!(reg & MCP794XX_BIT_ALMX_IF))
  561. goto out;
  562. reg &= ~MCP794XX_BIT_ALMX_IF;
  563. ret = i2c_smbus_write_byte_data(client, MCP794XX_REG_ALARM0_CTRL, reg);
  564. if (ret < 0)
  565. goto out;
  566. /* Disable alarm 0. */
  567. reg = i2c_smbus_read_byte_data(client, MCP794XX_REG_CONTROL);
  568. if (reg < 0)
  569. goto out;
  570. reg &= ~MCP794XX_BIT_ALM0_EN;
  571. ret = i2c_smbus_write_byte_data(client, MCP794XX_REG_CONTROL, reg);
  572. if (ret < 0)
  573. goto out;
  574. rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
  575. out:
  576. if (test_bit(HAS_ALARM, &ds1307->flags))
  577. enable_irq(client->irq);
  578. mutex_unlock(&ds1307->rtc->ops_lock);
  579. }
  580. static int mcp794xx_read_alarm(struct device *dev, struct rtc_wkalrm *t)
  581. {
  582. struct i2c_client *client = to_i2c_client(dev);
  583. struct ds1307 *ds1307 = i2c_get_clientdata(client);
  584. u8 *regs = ds1307->regs;
  585. int ret;
  586. if (!test_bit(HAS_ALARM, &ds1307->flags))
  587. return -EINVAL;
  588. /* Read control and alarm 0 registers. */
  589. ret = ds1307->read_block_data(client, MCP794XX_REG_CONTROL, 10, regs);
  590. if (ret < 0)
  591. return ret;
  592. t->enabled = !!(regs[0] & MCP794XX_BIT_ALM0_EN);
  593. /* Report alarm 0 time assuming 24-hour and day-of-month modes. */
  594. t->time.tm_sec = bcd2bin(ds1307->regs[3] & 0x7f);
  595. t->time.tm_min = bcd2bin(ds1307->regs[4] & 0x7f);
  596. t->time.tm_hour = bcd2bin(ds1307->regs[5] & 0x3f);
  597. t->time.tm_wday = bcd2bin(ds1307->regs[6] & 0x7) - 1;
  598. t->time.tm_mday = bcd2bin(ds1307->regs[7] & 0x3f);
  599. t->time.tm_mon = bcd2bin(ds1307->regs[8] & 0x1f) - 1;
  600. t->time.tm_year = -1;
  601. t->time.tm_yday = -1;
  602. t->time.tm_isdst = -1;
  603. dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
  604. "enabled=%d polarity=%d irq=%d match=%d\n", __func__,
  605. t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
  606. t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled,
  607. !!(ds1307->regs[6] & MCP794XX_BIT_ALMX_POL),
  608. !!(ds1307->regs[6] & MCP794XX_BIT_ALMX_IF),
  609. (ds1307->regs[6] & MCP794XX_MSK_ALMX_MATCH) >> 4);
  610. return 0;
  611. }
  612. static int mcp794xx_set_alarm(struct device *dev, struct rtc_wkalrm *t)
  613. {
  614. struct i2c_client *client = to_i2c_client(dev);
  615. struct ds1307 *ds1307 = i2c_get_clientdata(client);
  616. unsigned char *regs = ds1307->regs;
  617. int ret;
  618. if (!test_bit(HAS_ALARM, &ds1307->flags))
  619. return -EINVAL;
  620. dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
  621. "enabled=%d pending=%d\n", __func__,
  622. t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
  623. t->time.tm_wday, t->time.tm_mday, t->time.tm_mon,
  624. t->enabled, t->pending);
  625. /* Read control and alarm 0 registers. */
  626. ret = ds1307->read_block_data(client, MCP794XX_REG_CONTROL, 10, regs);
  627. if (ret < 0)
  628. return ret;
  629. /* Set alarm 0, using 24-hour and day-of-month modes. */
  630. regs[3] = bin2bcd(t->time.tm_sec);
  631. regs[4] = bin2bcd(t->time.tm_min);
  632. regs[5] = bin2bcd(t->time.tm_hour);
  633. regs[6] = bin2bcd(t->time.tm_wday) + 1;
  634. regs[7] = bin2bcd(t->time.tm_mday);
  635. regs[8] = bin2bcd(t->time.tm_mon) + 1;
  636. /* Clear the alarm 0 interrupt flag. */
  637. regs[6] &= ~MCP794XX_BIT_ALMX_IF;
  638. /* Set alarm match: second, minute, hour, day, date, month. */
  639. regs[6] |= MCP794XX_MSK_ALMX_MATCH;
  640. /* Disable interrupt. We will not enable until completely programmed */
  641. regs[0] &= ~MCP794XX_BIT_ALM0_EN;
  642. ret = ds1307->write_block_data(client, MCP794XX_REG_CONTROL, 10, regs);
  643. if (ret < 0)
  644. return ret;
  645. if (!t->enabled)
  646. return 0;
  647. regs[0] |= MCP794XX_BIT_ALM0_EN;
  648. return i2c_smbus_write_byte_data(client, MCP794XX_REG_CONTROL, regs[0]);
  649. }
  650. static int mcp794xx_alarm_irq_enable(struct device *dev, unsigned int enabled)
  651. {
  652. struct i2c_client *client = to_i2c_client(dev);
  653. struct ds1307 *ds1307 = i2c_get_clientdata(client);
  654. int reg;
  655. if (!test_bit(HAS_ALARM, &ds1307->flags))
  656. return -EINVAL;
  657. reg = i2c_smbus_read_byte_data(client, MCP794XX_REG_CONTROL);
  658. if (reg < 0)
  659. return reg;
  660. if (enabled)
  661. reg |= MCP794XX_BIT_ALM0_EN;
  662. else
  663. reg &= ~MCP794XX_BIT_ALM0_EN;
  664. return i2c_smbus_write_byte_data(client, MCP794XX_REG_CONTROL, reg);
  665. }
  666. static const struct rtc_class_ops mcp794xx_rtc_ops = {
  667. .read_time = ds1307_get_time,
  668. .set_time = ds1307_set_time,
  669. .read_alarm = mcp794xx_read_alarm,
  670. .set_alarm = mcp794xx_set_alarm,
  671. .alarm_irq_enable = mcp794xx_alarm_irq_enable,
  672. };
  673. /*----------------------------------------------------------------------*/
  674. static ssize_t
  675. ds1307_nvram_read(struct file *filp, struct kobject *kobj,
  676. struct bin_attribute *attr,
  677. char *buf, loff_t off, size_t count)
  678. {
  679. struct i2c_client *client;
  680. struct ds1307 *ds1307;
  681. int result;
  682. client = kobj_to_i2c_client(kobj);
  683. ds1307 = i2c_get_clientdata(client);
  684. if (unlikely(off >= ds1307->nvram->size))
  685. return 0;
  686. if ((off + count) > ds1307->nvram->size)
  687. count = ds1307->nvram->size - off;
  688. if (unlikely(!count))
  689. return count;
  690. result = ds1307->read_block_data(client, ds1307->nvram_offset + off,
  691. count, buf);
  692. if (result < 0)
  693. dev_err(&client->dev, "%s error %d\n", "nvram read", result);
  694. return result;
  695. }
  696. static ssize_t
  697. ds1307_nvram_write(struct file *filp, struct kobject *kobj,
  698. struct bin_attribute *attr,
  699. char *buf, loff_t off, size_t count)
  700. {
  701. struct i2c_client *client;
  702. struct ds1307 *ds1307;
  703. int result;
  704. client = kobj_to_i2c_client(kobj);
  705. ds1307 = i2c_get_clientdata(client);
  706. if (unlikely(off >= ds1307->nvram->size))
  707. return -EFBIG;
  708. if ((off + count) > ds1307->nvram->size)
  709. count = ds1307->nvram->size - off;
  710. if (unlikely(!count))
  711. return count;
  712. result = ds1307->write_block_data(client, ds1307->nvram_offset + off,
  713. count, buf);
  714. if (result < 0) {
  715. dev_err(&client->dev, "%s error %d\n", "nvram write", result);
  716. return result;
  717. }
  718. return count;
  719. }
  720. /*----------------------------------------------------------------------*/
  721. static u8 do_trickle_setup_ds1339(struct i2c_client *client,
  722. uint32_t ohms, bool diode)
  723. {
  724. u8 setup = (diode) ? DS1307_TRICKLE_CHARGER_DIODE :
  725. DS1307_TRICKLE_CHARGER_NO_DIODE;
  726. switch (ohms) {
  727. case 250:
  728. setup |= DS1307_TRICKLE_CHARGER_250_OHM;
  729. break;
  730. case 2000:
  731. setup |= DS1307_TRICKLE_CHARGER_2K_OHM;
  732. break;
  733. case 4000:
  734. setup |= DS1307_TRICKLE_CHARGER_4K_OHM;
  735. break;
  736. default:
  737. dev_warn(&client->dev,
  738. "Unsupported ohm value %u in dt\n", ohms);
  739. return 0;
  740. }
  741. return setup;
  742. }
  743. static void ds1307_trickle_of_init(struct i2c_client *client,
  744. struct chip_desc *chip)
  745. {
  746. uint32_t ohms = 0;
  747. bool diode = true;
  748. if (!chip->do_trickle_setup)
  749. goto out;
  750. if (of_property_read_u32(client->dev.of_node, "trickle-resistor-ohms" , &ohms))
  751. goto out;
  752. if (of_property_read_bool(client->dev.of_node, "trickle-diode-disable"))
  753. diode = false;
  754. chip->trickle_charger_setup = chip->do_trickle_setup(client,
  755. ohms, diode);
  756. out:
  757. return;
  758. }
  759. static int ds1307_probe(struct i2c_client *client,
  760. const struct i2c_device_id *id)
  761. {
  762. struct ds1307 *ds1307;
  763. int err = -ENODEV;
  764. int tmp;
  765. struct chip_desc *chip = &chips[id->driver_data];
  766. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  767. bool want_irq = false;
  768. unsigned char *buf;
  769. struct ds1307_platform_data *pdata = dev_get_platdata(&client->dev);
  770. static const int bbsqi_bitpos[] = {
  771. [ds_1337] = 0,
  772. [ds_1339] = DS1339_BIT_BBSQI,
  773. [ds_3231] = DS3231_BIT_BBSQW,
  774. };
  775. const struct rtc_class_ops *rtc_ops = &ds13xx_rtc_ops;
  776. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)
  777. && !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
  778. return -EIO;
  779. ds1307 = devm_kzalloc(&client->dev, sizeof(struct ds1307), GFP_KERNEL);
  780. if (!ds1307)
  781. return -ENOMEM;
  782. i2c_set_clientdata(client, ds1307);
  783. ds1307->client = client;
  784. ds1307->type = id->driver_data;
  785. if (!pdata && client->dev.of_node)
  786. ds1307_trickle_of_init(client, chip);
  787. else if (pdata && pdata->trickle_charger_setup)
  788. chip->trickle_charger_setup = pdata->trickle_charger_setup;
  789. if (chip->trickle_charger_setup && chip->trickle_charger_reg) {
  790. dev_dbg(&client->dev, "writing trickle charger info 0x%x to 0x%x\n",
  791. DS13XX_TRICKLE_CHARGER_MAGIC | chip->trickle_charger_setup,
  792. chip->trickle_charger_reg);
  793. i2c_smbus_write_byte_data(client, chip->trickle_charger_reg,
  794. DS13XX_TRICKLE_CHARGER_MAGIC |
  795. chip->trickle_charger_setup);
  796. }
  797. buf = ds1307->regs;
  798. if (i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) {
  799. ds1307->read_block_data = ds1307_native_smbus_read_block_data;
  800. ds1307->write_block_data = ds1307_native_smbus_write_block_data;
  801. } else {
  802. ds1307->read_block_data = ds1307_read_block_data;
  803. ds1307->write_block_data = ds1307_write_block_data;
  804. }
  805. switch (ds1307->type) {
  806. case ds_1337:
  807. case ds_1339:
  808. case ds_3231:
  809. /* get registers that the "rtc" read below won't read... */
  810. tmp = ds1307->read_block_data(ds1307->client,
  811. DS1337_REG_CONTROL, 2, buf);
  812. if (tmp != 2) {
  813. dev_dbg(&client->dev, "read error %d\n", tmp);
  814. err = -EIO;
  815. goto exit;
  816. }
  817. /* oscillator off? turn it on, so clock can tick. */
  818. if (ds1307->regs[0] & DS1337_BIT_nEOSC)
  819. ds1307->regs[0] &= ~DS1337_BIT_nEOSC;
  820. /*
  821. * Using IRQ? Disable the square wave and both alarms.
  822. * For some variants, be sure alarms can trigger when we're
  823. * running on Vbackup (BBSQI/BBSQW)
  824. */
  825. if (ds1307->client->irq > 0 && chip->alarm) {
  826. INIT_WORK(&ds1307->work, ds1307_work);
  827. ds1307->regs[0] |= DS1337_BIT_INTCN
  828. | bbsqi_bitpos[ds1307->type];
  829. ds1307->regs[0] &= ~(DS1337_BIT_A2IE | DS1337_BIT_A1IE);
  830. want_irq = true;
  831. }
  832. i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL,
  833. ds1307->regs[0]);
  834. /* oscillator fault? clear flag, and warn */
  835. if (ds1307->regs[1] & DS1337_BIT_OSF) {
  836. i2c_smbus_write_byte_data(client, DS1337_REG_STATUS,
  837. ds1307->regs[1] & ~DS1337_BIT_OSF);
  838. dev_warn(&client->dev, "SET TIME!\n");
  839. }
  840. break;
  841. case rx_8025:
  842. tmp = i2c_smbus_read_i2c_block_data(ds1307->client,
  843. RX8025_REG_CTRL1 << 4 | 0x08, 2, buf);
  844. if (tmp != 2) {
  845. dev_dbg(&client->dev, "read error %d\n", tmp);
  846. err = -EIO;
  847. goto exit;
  848. }
  849. /* oscillator off? turn it on, so clock can tick. */
  850. if (!(ds1307->regs[1] & RX8025_BIT_XST)) {
  851. ds1307->regs[1] |= RX8025_BIT_XST;
  852. i2c_smbus_write_byte_data(client,
  853. RX8025_REG_CTRL2 << 4 | 0x08,
  854. ds1307->regs[1]);
  855. dev_warn(&client->dev,
  856. "oscillator stop detected - SET TIME!\n");
  857. }
  858. if (ds1307->regs[1] & RX8025_BIT_PON) {
  859. ds1307->regs[1] &= ~RX8025_BIT_PON;
  860. i2c_smbus_write_byte_data(client,
  861. RX8025_REG_CTRL2 << 4 | 0x08,
  862. ds1307->regs[1]);
  863. dev_warn(&client->dev, "power-on detected\n");
  864. }
  865. if (ds1307->regs[1] & RX8025_BIT_VDET) {
  866. ds1307->regs[1] &= ~RX8025_BIT_VDET;
  867. i2c_smbus_write_byte_data(client,
  868. RX8025_REG_CTRL2 << 4 | 0x08,
  869. ds1307->regs[1]);
  870. dev_warn(&client->dev, "voltage drop detected\n");
  871. }
  872. /* make sure we are running in 24hour mode */
  873. if (!(ds1307->regs[0] & RX8025_BIT_2412)) {
  874. u8 hour;
  875. /* switch to 24 hour mode */
  876. i2c_smbus_write_byte_data(client,
  877. RX8025_REG_CTRL1 << 4 | 0x08,
  878. ds1307->regs[0] |
  879. RX8025_BIT_2412);
  880. tmp = i2c_smbus_read_i2c_block_data(ds1307->client,
  881. RX8025_REG_CTRL1 << 4 | 0x08, 2, buf);
  882. if (tmp != 2) {
  883. dev_dbg(&client->dev, "read error %d\n", tmp);
  884. err = -EIO;
  885. goto exit;
  886. }
  887. /* correct hour */
  888. hour = bcd2bin(ds1307->regs[DS1307_REG_HOUR]);
  889. if (hour == 12)
  890. hour = 0;
  891. if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
  892. hour += 12;
  893. i2c_smbus_write_byte_data(client,
  894. DS1307_REG_HOUR << 4 | 0x08,
  895. hour);
  896. }
  897. break;
  898. case ds_1388:
  899. ds1307->offset = 1; /* Seconds starts at 1 */
  900. break;
  901. case mcp794xx:
  902. rtc_ops = &mcp794xx_rtc_ops;
  903. if (ds1307->client->irq > 0 && chip->alarm) {
  904. INIT_WORK(&ds1307->work, mcp794xx_work);
  905. want_irq = true;
  906. }
  907. break;
  908. default:
  909. break;
  910. }
  911. read_rtc:
  912. /* read RTC registers */
  913. tmp = ds1307->read_block_data(ds1307->client, ds1307->offset, 8, buf);
  914. if (tmp != 8) {
  915. dev_dbg(&client->dev, "read error %d\n", tmp);
  916. err = -EIO;
  917. goto exit;
  918. }
  919. /*
  920. * minimal sanity checking; some chips (like DS1340) don't
  921. * specify the extra bits as must-be-zero, but there are
  922. * still a few values that are clearly out-of-range.
  923. */
  924. tmp = ds1307->regs[DS1307_REG_SECS];
  925. switch (ds1307->type) {
  926. case ds_1307:
  927. case m41t00:
  928. /* clock halted? turn it on, so clock can tick. */
  929. if (tmp & DS1307_BIT_CH) {
  930. i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
  931. dev_warn(&client->dev, "SET TIME!\n");
  932. goto read_rtc;
  933. }
  934. break;
  935. case ds_1338:
  936. /* clock halted? turn it on, so clock can tick. */
  937. if (tmp & DS1307_BIT_CH)
  938. i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
  939. /* oscillator fault? clear flag, and warn */
  940. if (ds1307->regs[DS1307_REG_CONTROL] & DS1338_BIT_OSF) {
  941. i2c_smbus_write_byte_data(client, DS1307_REG_CONTROL,
  942. ds1307->regs[DS1307_REG_CONTROL]
  943. & ~DS1338_BIT_OSF);
  944. dev_warn(&client->dev, "SET TIME!\n");
  945. goto read_rtc;
  946. }
  947. break;
  948. case ds_1340:
  949. /* clock halted? turn it on, so clock can tick. */
  950. if (tmp & DS1340_BIT_nEOSC)
  951. i2c_smbus_write_byte_data(client, DS1307_REG_SECS, 0);
  952. tmp = i2c_smbus_read_byte_data(client, DS1340_REG_FLAG);
  953. if (tmp < 0) {
  954. dev_dbg(&client->dev, "read error %d\n", tmp);
  955. err = -EIO;
  956. goto exit;
  957. }
  958. /* oscillator fault? clear flag, and warn */
  959. if (tmp & DS1340_BIT_OSF) {
  960. i2c_smbus_write_byte_data(client, DS1340_REG_FLAG, 0);
  961. dev_warn(&client->dev, "SET TIME!\n");
  962. }
  963. break;
  964. case mcp794xx:
  965. /* make sure that the backup battery is enabled */
  966. if (!(ds1307->regs[DS1307_REG_WDAY] & MCP794XX_BIT_VBATEN)) {
  967. i2c_smbus_write_byte_data(client, DS1307_REG_WDAY,
  968. ds1307->regs[DS1307_REG_WDAY]
  969. | MCP794XX_BIT_VBATEN);
  970. }
  971. /* clock halted? turn it on, so clock can tick. */
  972. if (!(tmp & MCP794XX_BIT_ST)) {
  973. i2c_smbus_write_byte_data(client, DS1307_REG_SECS,
  974. MCP794XX_BIT_ST);
  975. dev_warn(&client->dev, "SET TIME!\n");
  976. goto read_rtc;
  977. }
  978. break;
  979. default:
  980. break;
  981. }
  982. tmp = ds1307->regs[DS1307_REG_HOUR];
  983. switch (ds1307->type) {
  984. case ds_1340:
  985. case m41t00:
  986. /*
  987. * NOTE: ignores century bits; fix before deploying
  988. * systems that will run through year 2100.
  989. */
  990. break;
  991. case rx_8025:
  992. break;
  993. default:
  994. if (!(tmp & DS1307_BIT_12HR))
  995. break;
  996. /*
  997. * Be sure we're in 24 hour mode. Multi-master systems
  998. * take note...
  999. */
  1000. tmp = bcd2bin(tmp & 0x1f);
  1001. if (tmp == 12)
  1002. tmp = 0;
  1003. if (ds1307->regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
  1004. tmp += 12;
  1005. i2c_smbus_write_byte_data(client,
  1006. ds1307->offset + DS1307_REG_HOUR,
  1007. bin2bcd(tmp));
  1008. }
  1009. device_set_wakeup_capable(&client->dev, want_irq);
  1010. ds1307->rtc = devm_rtc_device_register(&client->dev, client->name,
  1011. rtc_ops, THIS_MODULE);
  1012. if (IS_ERR(ds1307->rtc)) {
  1013. return PTR_ERR(ds1307->rtc);
  1014. }
  1015. if (want_irq) {
  1016. err = request_irq(client->irq, ds1307_irq, IRQF_SHARED,
  1017. ds1307->rtc->name, client);
  1018. if (err) {
  1019. client->irq = 0;
  1020. dev_err(&client->dev, "unable to request IRQ!\n");
  1021. } else {
  1022. set_bit(HAS_ALARM, &ds1307->flags);
  1023. dev_dbg(&client->dev, "got IRQ %d\n", client->irq);
  1024. }
  1025. }
  1026. if (chip->nvram_size) {
  1027. ds1307->nvram = devm_kzalloc(&client->dev,
  1028. sizeof(struct bin_attribute),
  1029. GFP_KERNEL);
  1030. if (!ds1307->nvram) {
  1031. dev_err(&client->dev, "cannot allocate memory for nvram sysfs\n");
  1032. } else {
  1033. ds1307->nvram->attr.name = "nvram";
  1034. ds1307->nvram->attr.mode = S_IRUGO | S_IWUSR;
  1035. sysfs_bin_attr_init(ds1307->nvram);
  1036. ds1307->nvram->read = ds1307_nvram_read;
  1037. ds1307->nvram->write = ds1307_nvram_write;
  1038. ds1307->nvram->size = chip->nvram_size;
  1039. ds1307->nvram_offset = chip->nvram_offset;
  1040. err = sysfs_create_bin_file(&client->dev.kobj,
  1041. ds1307->nvram);
  1042. if (err) {
  1043. dev_err(&client->dev,
  1044. "unable to create sysfs file: %s\n",
  1045. ds1307->nvram->attr.name);
  1046. } else {
  1047. set_bit(HAS_NVRAM, &ds1307->flags);
  1048. dev_info(&client->dev, "%zu bytes nvram\n",
  1049. ds1307->nvram->size);
  1050. }
  1051. }
  1052. }
  1053. return 0;
  1054. exit:
  1055. return err;
  1056. }
  1057. static int ds1307_remove(struct i2c_client *client)
  1058. {
  1059. struct ds1307 *ds1307 = i2c_get_clientdata(client);
  1060. if (test_and_clear_bit(HAS_ALARM, &ds1307->flags)) {
  1061. free_irq(client->irq, client);
  1062. cancel_work_sync(&ds1307->work);
  1063. }
  1064. if (test_and_clear_bit(HAS_NVRAM, &ds1307->flags))
  1065. sysfs_remove_bin_file(&client->dev.kobj, ds1307->nvram);
  1066. return 0;
  1067. }
  1068. static struct i2c_driver ds1307_driver = {
  1069. .driver = {
  1070. .name = "rtc-ds1307",
  1071. .owner = THIS_MODULE,
  1072. },
  1073. .probe = ds1307_probe,
  1074. .remove = ds1307_remove,
  1075. .id_table = ds1307_id,
  1076. };
  1077. module_i2c_driver(ds1307_driver);
  1078. MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips");
  1079. MODULE_LICENSE("GPL");