x1205.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /*
  2. * x1205.c - An i2c driver for the Xicor X1205 RTC
  3. * Copyright 2004 Karen Spearel
  4. * Copyright 2005 Alessandro Zummo
  5. *
  6. * please send all reports to:
  7. * kas11 at tampabay dot rr dot com
  8. * a dot zummo at towertech dot it
  9. *
  10. * based on the other drivers in this same directory.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/slab.h>
  20. #include <linux/i2c.h>
  21. #include <linux/string.h>
  22. #include <linux/bcd.h>
  23. #include <linux/rtc.h>
  24. #include <linux/list.h>
  25. #include <linux/x1205.h>
  26. #define DRV_VERSION "0.9.9"
  27. /* Addresses to scan: none. This chip is located at
  28. * 0x6f and uses a two bytes register addressing.
  29. * Two bytes need to be written to read a single register,
  30. * while most other chips just require one and take the second
  31. * one as the data to be written. To prevent corrupting
  32. * unknown chips, the user must explicitely set the probe parameter.
  33. */
  34. static unsigned short normal_i2c[] = { I2C_CLIENT_END };
  35. /* Insmod parameters */
  36. I2C_CLIENT_INSMOD;
  37. I2C_CLIENT_MODULE_PARM(hctosys,
  38. "Set the system time from the hardware clock upon initialization");
  39. /* offsets into CCR area */
  40. #define CCR_SEC 0
  41. #define CCR_MIN 1
  42. #define CCR_HOUR 2
  43. #define CCR_MDAY 3
  44. #define CCR_MONTH 4
  45. #define CCR_YEAR 5
  46. #define CCR_WDAY 6
  47. #define CCR_Y2K 7
  48. #define X1205_REG_SR 0x3F /* status register */
  49. #define X1205_REG_Y2K 0x37
  50. #define X1205_REG_DW 0x36
  51. #define X1205_REG_YR 0x35
  52. #define X1205_REG_MO 0x34
  53. #define X1205_REG_DT 0x33
  54. #define X1205_REG_HR 0x32
  55. #define X1205_REG_MN 0x31
  56. #define X1205_REG_SC 0x30
  57. #define X1205_REG_DTR 0x13
  58. #define X1205_REG_ATR 0x12
  59. #define X1205_REG_INT 0x11
  60. #define X1205_REG_0 0x10
  61. #define X1205_REG_Y2K1 0x0F
  62. #define X1205_REG_DWA1 0x0E
  63. #define X1205_REG_YRA1 0x0D
  64. #define X1205_REG_MOA1 0x0C
  65. #define X1205_REG_DTA1 0x0B
  66. #define X1205_REG_HRA1 0x0A
  67. #define X1205_REG_MNA1 0x09
  68. #define X1205_REG_SCA1 0x08
  69. #define X1205_REG_Y2K0 0x07
  70. #define X1205_REG_DWA0 0x06
  71. #define X1205_REG_YRA0 0x05
  72. #define X1205_REG_MOA0 0x04
  73. #define X1205_REG_DTA0 0x03
  74. #define X1205_REG_HRA0 0x02
  75. #define X1205_REG_MNA0 0x01
  76. #define X1205_REG_SCA0 0x00
  77. #define X1205_CCR_BASE 0x30 /* Base address of CCR */
  78. #define X1205_ALM0_BASE 0x00 /* Base address of ALARM0 */
  79. #define X1205_SR_RTCF 0x01 /* Clock failure */
  80. #define X1205_SR_WEL 0x02 /* Write Enable Latch */
  81. #define X1205_SR_RWEL 0x04 /* Register Write Enable */
  82. #define X1205_DTR_DTR0 0x01
  83. #define X1205_DTR_DTR1 0x02
  84. #define X1205_DTR_DTR2 0x04
  85. #define X1205_HR_MIL 0x80 /* Set in ccr.hour for 24 hr mode */
  86. /* Prototypes */
  87. static int x1205_attach(struct i2c_adapter *adapter);
  88. static int x1205_detach(struct i2c_client *client);
  89. static int x1205_probe(struct i2c_adapter *adapter, int address, int kind);
  90. static int x1205_command(struct i2c_client *client, unsigned int cmd,
  91. void *arg);
  92. static struct i2c_driver x1205_driver = {
  93. .driver = {
  94. .name = "x1205",
  95. },
  96. .attach_adapter = &x1205_attach,
  97. .detach_client = &x1205_detach,
  98. };
  99. struct x1205_data {
  100. struct i2c_client client;
  101. struct list_head list;
  102. unsigned int epoch;
  103. };
  104. static const unsigned char days_in_mo[] =
  105. { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  106. static LIST_HEAD(x1205_clients);
  107. /* Workaround until the I2C subsytem will allow to send
  108. * commands to a specific client. This function will send the command
  109. * to the first client.
  110. */
  111. int x1205_do_command(unsigned int cmd, void *arg)
  112. {
  113. struct list_head *walk;
  114. struct list_head *tmp;
  115. struct x1205_data *data;
  116. list_for_each_safe(walk, tmp, &x1205_clients) {
  117. data = list_entry(walk, struct x1205_data, list);
  118. return x1205_command(&data->client, cmd, arg);
  119. }
  120. return -ENODEV;
  121. }
  122. #define is_leap(year) \
  123. ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
  124. /* make sure the rtc_time values are in bounds */
  125. static int x1205_validate_tm(struct rtc_time *tm)
  126. {
  127. int year = tm->tm_year + 1900;
  128. if ((tm->tm_year < 70) || (tm->tm_year > 255))
  129. return -EINVAL;
  130. if ((tm->tm_mon > 11) || (tm->tm_mday == 0))
  131. return -EINVAL;
  132. if (tm->tm_mday > days_in_mo[tm->tm_mon]
  133. + ((tm->tm_mon == 1) && is_leap(year)))
  134. return -EINVAL;
  135. if ((tm->tm_hour >= 24) || (tm->tm_min >= 60) || (tm->tm_sec >= 60))
  136. return -EINVAL;
  137. return 0;
  138. }
  139. /*
  140. * In the routines that deal directly with the x1205 hardware, we use
  141. * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch
  142. * Epoch is initialized as 2000. Time is set to UTC.
  143. */
  144. static int x1205_get_datetime(struct i2c_client *client, struct rtc_time *tm,
  145. u8 reg_base)
  146. {
  147. unsigned char dt_addr[2] = { 0, reg_base };
  148. static unsigned char sr_addr[2] = { 0, X1205_REG_SR };
  149. unsigned char buf[8], sr;
  150. struct i2c_msg msgs[] = {
  151. { client->addr, 0, 2, sr_addr }, /* setup read ptr */
  152. { client->addr, I2C_M_RD, 1, &sr }, /* read status */
  153. { client->addr, 0, 2, dt_addr }, /* setup read ptr */
  154. { client->addr, I2C_M_RD, 8, buf }, /* read date */
  155. };
  156. struct x1205_data *data = i2c_get_clientdata(client);
  157. /* read status register */
  158. if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) {
  159. dev_err(&client->dev, "%s: read error\n", __FUNCTION__);
  160. return -EIO;
  161. }
  162. /* check for battery failure */
  163. if (sr & X1205_SR_RTCF) {
  164. dev_warn(&client->dev,
  165. "Clock had a power failure, you must set the date.\n");
  166. return -EINVAL;
  167. }
  168. /* read date registers */
  169. if ((i2c_transfer(client->adapter, &msgs[2], 2)) != 2) {
  170. dev_err(&client->dev, "%s: read error\n", __FUNCTION__);
  171. return -EIO;
  172. }
  173. dev_dbg(&client->dev,
  174. "%s: raw read data - sec=%02x, min=%02x, hr=%02x, "
  175. "mday=%02x, mon=%02x, year=%02x, wday=%02x, y2k=%02x\n",
  176. __FUNCTION__,
  177. buf[0], buf[1], buf[2], buf[3],
  178. buf[4], buf[5], buf[6], buf[7]);
  179. tm->tm_sec = BCD2BIN(buf[CCR_SEC]);
  180. tm->tm_min = BCD2BIN(buf[CCR_MIN]);
  181. tm->tm_hour = BCD2BIN(buf[CCR_HOUR] & 0x3F); /* hr is 0-23 */
  182. tm->tm_mday = BCD2BIN(buf[CCR_MDAY]);
  183. tm->tm_mon = BCD2BIN(buf[CCR_MONTH]);
  184. data->epoch = BCD2BIN(buf[CCR_Y2K]) * 100;
  185. tm->tm_year = BCD2BIN(buf[CCR_YEAR]) + data->epoch - 1900;
  186. tm->tm_wday = buf[CCR_WDAY];
  187. dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
  188. "mday=%d, mon=%d, year=%d, wday=%d\n",
  189. __FUNCTION__,
  190. tm->tm_sec, tm->tm_min, tm->tm_hour,
  191. tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
  192. return 0;
  193. }
  194. static int x1205_set_datetime(struct i2c_client *client, struct rtc_time *tm,
  195. int datetoo, u8 reg_base)
  196. {
  197. int i, err, xfer;
  198. unsigned char buf[8];
  199. static const unsigned char wel[3] = { 0, X1205_REG_SR,
  200. X1205_SR_WEL };
  201. static const unsigned char rwel[3] = { 0, X1205_REG_SR,
  202. X1205_SR_WEL | X1205_SR_RWEL };
  203. static const unsigned char diswe[3] = { 0, X1205_REG_SR, 0 };
  204. struct x1205_data *data = i2c_get_clientdata(client);
  205. /* check if all values in the tm struct are correct */
  206. if ((err = x1205_validate_tm(tm)) < 0)
  207. return err;
  208. dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, "
  209. "mday=%d, mon=%d, year=%d, wday=%d\n",
  210. __FUNCTION__,
  211. tm->tm_sec, tm->tm_min, tm->tm_hour,
  212. tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
  213. buf[CCR_SEC] = BIN2BCD(tm->tm_sec);
  214. buf[CCR_MIN] = BIN2BCD(tm->tm_min);
  215. /* set hour and 24hr bit */
  216. buf[CCR_HOUR] = BIN2BCD(tm->tm_hour) | X1205_HR_MIL;
  217. /* should we also set the date? */
  218. if (datetoo) {
  219. buf[CCR_MDAY] = BIN2BCD(tm->tm_mday);
  220. /* month, 0 - 11 */
  221. buf[CCR_MONTH] = BIN2BCD(tm->tm_mon);
  222. /* year, since 1900 */
  223. buf[CCR_YEAR] = BIN2BCD(tm->tm_year + 1900 - data->epoch);
  224. buf[CCR_WDAY] = tm->tm_wday & 0x07;
  225. buf[CCR_Y2K] = BIN2BCD(data->epoch / 100);
  226. }
  227. /* this sequence is required to unlock the chip */
  228. xfer = i2c_master_send(client, wel, 3);
  229. if (xfer != 3) {
  230. dev_err(&client->dev, "%s: wel - %d\n", __FUNCTION__, xfer);
  231. return -EIO;
  232. }
  233. xfer = i2c_master_send(client, rwel, 3);
  234. if (xfer != 3) {
  235. dev_err(&client->dev, "%s: rwel - %d\n", __FUNCTION__, xfer);
  236. return -EIO;
  237. }
  238. /* write register's data */
  239. for (i = 0; i < (datetoo ? 8 : 3); i++) {
  240. unsigned char rdata[3] = { 0, reg_base + i, buf[i] };
  241. xfer = i2c_master_send(client, rdata, 3);
  242. if (xfer != 3) {
  243. dev_err(&client->dev,
  244. "%s: xfer=%d addr=%02x, data=%02x\n",
  245. __FUNCTION__,
  246. xfer, rdata[1], rdata[2]);
  247. return -EIO;
  248. }
  249. };
  250. /* disable further writes */
  251. xfer = i2c_master_send(client, diswe, 3);
  252. if (xfer != 3) {
  253. dev_err(&client->dev, "%s: diswe - %d\n", __FUNCTION__, xfer);
  254. return -EIO;
  255. }
  256. return 0;
  257. }
  258. static int x1205_get_dtrim(struct i2c_client *client, int *trim)
  259. {
  260. unsigned char dtr;
  261. static unsigned char dtr_addr[2] = { 0, X1205_REG_DTR };
  262. struct i2c_msg msgs[] = {
  263. { client->addr, 0, 2, dtr_addr }, /* setup read ptr */
  264. { client->addr, I2C_M_RD, 1, &dtr }, /* read dtr */
  265. };
  266. /* read dtr register */
  267. if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) {
  268. dev_err(&client->dev, "%s: read error\n", __FUNCTION__);
  269. return -EIO;
  270. }
  271. dev_dbg(&client->dev, "%s: raw dtr=%x\n", __FUNCTION__, dtr);
  272. *trim = 0;
  273. if (dtr & X1205_DTR_DTR0)
  274. *trim += 20;
  275. if (dtr & X1205_DTR_DTR1)
  276. *trim += 10;
  277. if (dtr & X1205_DTR_DTR2)
  278. *trim = -*trim;
  279. return 0;
  280. }
  281. static int x1205_get_atrim(struct i2c_client *client, int *trim)
  282. {
  283. s8 atr;
  284. static unsigned char atr_addr[2] = { 0, X1205_REG_ATR };
  285. struct i2c_msg msgs[] = {
  286. { client->addr, 0, 2, atr_addr }, /* setup read ptr */
  287. { client->addr, I2C_M_RD, 1, &atr }, /* read atr */
  288. };
  289. /* read atr register */
  290. if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) {
  291. dev_err(&client->dev, "%s: read error\n", __FUNCTION__);
  292. return -EIO;
  293. }
  294. dev_dbg(&client->dev, "%s: raw atr=%x\n", __FUNCTION__, atr);
  295. /* atr is a two's complement value on 6 bits,
  296. * perform sign extension. The formula is
  297. * Catr = (atr * 0.25pF) + 11.00pF.
  298. */
  299. if (atr & 0x20)
  300. atr |= 0xC0;
  301. dev_dbg(&client->dev, "%s: raw atr=%x (%d)\n", __FUNCTION__, atr, atr);
  302. *trim = (atr * 250) + 11000;
  303. dev_dbg(&client->dev, "%s: real=%d\n", __FUNCTION__, *trim);
  304. return 0;
  305. }
  306. static int x1205_hctosys(struct i2c_client *client)
  307. {
  308. int err;
  309. struct rtc_time tm;
  310. struct timespec tv;
  311. err = x1205_command(client, X1205_CMD_GETDATETIME, &tm);
  312. if (err) {
  313. dev_err(&client->dev,
  314. "Unable to set the system clock\n");
  315. return err;
  316. }
  317. /* IMPORTANT: the RTC only stores whole seconds. It is arbitrary
  318. * whether it stores the most close value or the value with partial
  319. * seconds truncated. However, it is important that we use it to store
  320. * the truncated value. This is because otherwise it is necessary,
  321. * in an rtc sync function, to read both xtime.tv_sec and
  322. * xtime.tv_nsec. On some processors (i.e. ARM), an atomic read
  323. * of >32bits is not possible. So storing the most close value would
  324. * slow down the sync API. So here we have the truncated value and
  325. * the best guess is to add 0.5s.
  326. */
  327. tv.tv_nsec = NSEC_PER_SEC >> 1;
  328. /* WARNING: this is not the C library 'mktime' call, it is a built in
  329. * inline function from include/linux/time.h. It expects (requires)
  330. * the month to be in the range 1-12
  331. */
  332. tv.tv_sec = mktime(tm.tm_year + 1900, tm.tm_mon + 1,
  333. tm.tm_mday, tm.tm_hour,
  334. tm.tm_min, tm.tm_sec);
  335. do_settimeofday(&tv);
  336. dev_info(&client->dev,
  337. "setting the system clock to %d-%d-%d %d:%d:%d\n",
  338. tm.tm_year + 1900, tm.tm_mon + 1,
  339. tm.tm_mday, tm.tm_hour, tm.tm_min,
  340. tm.tm_sec);
  341. return 0;
  342. }
  343. struct x1205_limit
  344. {
  345. unsigned char reg;
  346. unsigned char mask;
  347. unsigned char min;
  348. unsigned char max;
  349. };
  350. static int x1205_validate_client(struct i2c_client *client)
  351. {
  352. int i, xfer;
  353. /* Probe array. We will read the register at the specified
  354. * address and check if the given bits are zero.
  355. */
  356. static const unsigned char probe_zero_pattern[] = {
  357. /* register, mask */
  358. X1205_REG_SR, 0x18,
  359. X1205_REG_DTR, 0xF8,
  360. X1205_REG_ATR, 0xC0,
  361. X1205_REG_INT, 0x18,
  362. X1205_REG_0, 0xFF,
  363. };
  364. static const struct x1205_limit probe_limits_pattern[] = {
  365. /* register, mask, min, max */
  366. { X1205_REG_Y2K, 0xFF, 19, 20 },
  367. { X1205_REG_DW, 0xFF, 0, 6 },
  368. { X1205_REG_YR, 0xFF, 0, 99 },
  369. { X1205_REG_MO, 0xFF, 0, 12 },
  370. { X1205_REG_DT, 0xFF, 0, 31 },
  371. { X1205_REG_HR, 0x7F, 0, 23 },
  372. { X1205_REG_MN, 0xFF, 0, 59 },
  373. { X1205_REG_SC, 0xFF, 0, 59 },
  374. { X1205_REG_Y2K1, 0xFF, 19, 20 },
  375. { X1205_REG_Y2K0, 0xFF, 19, 20 },
  376. };
  377. /* check that registers have bits a 0 where expected */
  378. for (i = 0; i < ARRAY_SIZE(probe_zero_pattern); i += 2) {
  379. unsigned char buf;
  380. unsigned char addr[2] = { 0, probe_zero_pattern[i] };
  381. struct i2c_msg msgs[2] = {
  382. { client->addr, 0, 2, addr },
  383. { client->addr, I2C_M_RD, 1, &buf },
  384. };
  385. xfer = i2c_transfer(client->adapter, msgs, 2);
  386. if (xfer != 2) {
  387. dev_err(&client->adapter->dev,
  388. "%s: could not read register %x\n",
  389. __FUNCTION__, addr[1]);
  390. return -EIO;
  391. }
  392. if ((buf & probe_zero_pattern[i+1]) != 0) {
  393. dev_err(&client->adapter->dev,
  394. "%s: register=%02x, zero pattern=%d, value=%x\n",
  395. __FUNCTION__, addr[1], i, buf);
  396. return -ENODEV;
  397. }
  398. }
  399. /* check limits (only registers with bcd values) */
  400. for (i = 0; i < ARRAY_SIZE(probe_limits_pattern); i++) {
  401. unsigned char reg, value;
  402. unsigned char addr[2] = { 0, probe_limits_pattern[i].reg };
  403. struct i2c_msg msgs[2] = {
  404. { client->addr, 0, 2, addr },
  405. { client->addr, I2C_M_RD, 1, &reg },
  406. };
  407. xfer = i2c_transfer(client->adapter, msgs, 2);
  408. if (xfer != 2) {
  409. dev_err(&client->adapter->dev,
  410. "%s: could not read register %x\n",
  411. __FUNCTION__, addr[1]);
  412. return -EIO;
  413. }
  414. value = BCD2BIN(reg & probe_limits_pattern[i].mask);
  415. if (value > probe_limits_pattern[i].max ||
  416. value < probe_limits_pattern[i].min) {
  417. dev_dbg(&client->adapter->dev,
  418. "%s: register=%x, lim pattern=%d, value=%d\n",
  419. __FUNCTION__, addr[1], i, value);
  420. return -ENODEV;
  421. }
  422. }
  423. return 0;
  424. }
  425. static int x1205_attach(struct i2c_adapter *adapter)
  426. {
  427. dev_dbg(&adapter->dev, "%s\n", __FUNCTION__);
  428. return i2c_probe(adapter, &addr_data, x1205_probe);
  429. }
  430. int x1205_direct_attach(int adapter_id,
  431. struct i2c_client_address_data *address_data)
  432. {
  433. int err;
  434. struct i2c_adapter *adapter = i2c_get_adapter(adapter_id);
  435. if (adapter) {
  436. err = i2c_probe(adapter,
  437. address_data, x1205_probe);
  438. i2c_put_adapter(adapter);
  439. return err;
  440. }
  441. return -ENODEV;
  442. }
  443. static int x1205_probe(struct i2c_adapter *adapter, int address, int kind)
  444. {
  445. struct i2c_client *client;
  446. struct x1205_data *data;
  447. int err = 0;
  448. dev_dbg(&adapter->dev, "%s\n", __FUNCTION__);
  449. if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
  450. err = -ENODEV;
  451. goto exit;
  452. }
  453. if (!(data = kzalloc(sizeof(struct x1205_data), GFP_KERNEL))) {
  454. err = -ENOMEM;
  455. goto exit;
  456. }
  457. /* Initialize our structures */
  458. data->epoch = 2000;
  459. client = &data->client;
  460. client->addr = address;
  461. client->driver = &x1205_driver;
  462. client->adapter = adapter;
  463. strlcpy(client->name, "x1205", I2C_NAME_SIZE);
  464. i2c_set_clientdata(client, data);
  465. /* Verify the chip is really an X1205 */
  466. if (kind < 0) {
  467. if (x1205_validate_client(client) < 0) {
  468. err = -ENODEV;
  469. goto exit_kfree;
  470. }
  471. }
  472. /* Inform the i2c layer */
  473. if ((err = i2c_attach_client(client)))
  474. goto exit_kfree;
  475. list_add(&data->list, &x1205_clients);
  476. dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n");
  477. /* If requested, set the system time */
  478. if (hctosys)
  479. x1205_hctosys(client);
  480. return 0;
  481. exit_kfree:
  482. kfree(data);
  483. exit:
  484. return err;
  485. }
  486. static int x1205_detach(struct i2c_client *client)
  487. {
  488. int err;
  489. struct x1205_data *data = i2c_get_clientdata(client);
  490. dev_dbg(&client->dev, "%s\n", __FUNCTION__);
  491. if ((err = i2c_detach_client(client)))
  492. return err;
  493. list_del(&data->list);
  494. kfree(data);
  495. return 0;
  496. }
  497. static int x1205_command(struct i2c_client *client, unsigned int cmd,
  498. void *param)
  499. {
  500. if (param == NULL)
  501. return -EINVAL;
  502. if (!capable(CAP_SYS_TIME))
  503. return -EACCES;
  504. dev_dbg(&client->dev, "%s: cmd=%d\n", __FUNCTION__, cmd);
  505. switch (cmd) {
  506. case X1205_CMD_GETDATETIME:
  507. return x1205_get_datetime(client, param, X1205_CCR_BASE);
  508. case X1205_CMD_SETTIME:
  509. return x1205_set_datetime(client, param, 0,
  510. X1205_CCR_BASE);
  511. case X1205_CMD_SETDATETIME:
  512. return x1205_set_datetime(client, param, 1,
  513. X1205_CCR_BASE);
  514. case X1205_CMD_GETALARM:
  515. return x1205_get_datetime(client, param, X1205_ALM0_BASE);
  516. case X1205_CMD_SETALARM:
  517. return x1205_set_datetime(client, param, 1,
  518. X1205_ALM0_BASE);
  519. case X1205_CMD_GETDTRIM:
  520. return x1205_get_dtrim(client, param);
  521. case X1205_CMD_GETATRIM:
  522. return x1205_get_atrim(client, param);
  523. default:
  524. return -EINVAL;
  525. }
  526. }
  527. static int __init x1205_init(void)
  528. {
  529. return i2c_add_driver(&x1205_driver);
  530. }
  531. static void __exit x1205_exit(void)
  532. {
  533. i2c_del_driver(&x1205_driver);
  534. }
  535. MODULE_AUTHOR(
  536. "Karen Spearel <kas11@tampabay.rr.com>, "
  537. "Alessandro Zummo <a.zummo@towertech.it>");
  538. MODULE_DESCRIPTION("Xicor X1205 RTC driver");
  539. MODULE_LICENSE("GPL");
  540. MODULE_VERSION(DRV_VERSION);
  541. EXPORT_SYMBOL_GPL(x1205_do_command);
  542. EXPORT_SYMBOL_GPL(x1205_direct_attach);
  543. module_init(x1205_init);
  544. module_exit(x1205_exit);