i2c-keywest.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. /*
  2. i2c Support for Apple Keywest I2C Bus Controller
  3. Copyright (c) 2001 Benjamin Herrenschmidt <benh@kernel.crashing.org>
  4. Original work by
  5. Copyright (c) 2000 Philip Edelbrock <phil@stimpy.netroedge.com>
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. Changes:
  18. 2001/12/13 BenH New implementation
  19. 2001/12/15 BenH Add support for "byte" and "quick"
  20. transfers. Add i2c_xfer routine.
  21. 2003/09/21 BenH Rework state machine with Paulus help
  22. 2004/01/21 BenH Merge in Greg KH changes, polled mode is back
  23. 2004/02/05 BenH Merge 64 bits fixes from the g5 ppc64 tree
  24. My understanding of the various modes supported by keywest are:
  25. - Dumb mode : not implemented, probably direct tweaking of lines
  26. - Standard mode : simple i2c transaction of type
  27. S Addr R/W A Data A Data ... T
  28. - Standard sub mode : combined 8 bit subaddr write with data read
  29. S Addr R/W A SubAddr A Data A Data ... T
  30. - Combined mode : Subaddress and Data sequences appended with no stop
  31. S Addr R/W A SubAddr S Addr R/W A Data A Data ... T
  32. Currently, this driver uses only Standard mode for i2c xfer, and
  33. smbus byte & quick transfers ; and uses StandardSub mode for
  34. other smbus transfers instead of combined as we need that for the
  35. sound driver to be happy
  36. */
  37. #include <linux/module.h>
  38. #include <linux/kernel.h>
  39. #include <linux/ioport.h>
  40. #include <linux/pci.h>
  41. #include <linux/types.h>
  42. #include <linux/delay.h>
  43. #include <linux/i2c.h>
  44. #include <linux/init.h>
  45. #include <linux/mm.h>
  46. #include <linux/timer.h>
  47. #include <linux/spinlock.h>
  48. #include <linux/completion.h>
  49. #include <linux/interrupt.h>
  50. #include <asm/io.h>
  51. #include <asm/prom.h>
  52. #include <asm/machdep.h>
  53. #include <asm/pmac_feature.h>
  54. #include <asm/pmac_low_i2c.h>
  55. #include "i2c-keywest.h"
  56. #undef POLLED_MODE
  57. /* Some debug macros */
  58. #define WRONG_STATE(name) do {\
  59. pr_debug("KW: wrong state. Got %s, state: %s (isr: %02x)\n", \
  60. name, __kw_state_names[iface->state], isr); \
  61. } while(0)
  62. #ifdef DEBUG
  63. static const char *__kw_state_names[] = {
  64. "state_idle",
  65. "state_addr",
  66. "state_read",
  67. "state_write",
  68. "state_stop",
  69. "state_dead"
  70. };
  71. #endif /* DEBUG */
  72. MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
  73. MODULE_DESCRIPTION("I2C driver for Apple's Keywest");
  74. MODULE_LICENSE("GPL");
  75. #ifdef POLLED_MODE
  76. /* Don't schedule, the g5 fan controller is too
  77. * timing sensitive
  78. */
  79. static u8
  80. wait_interrupt(struct keywest_iface* iface)
  81. {
  82. int i;
  83. u8 isr;
  84. for (i = 0; i < 200000; i++) {
  85. isr = read_reg(reg_isr) & KW_I2C_IRQ_MASK;
  86. if (isr != 0)
  87. return isr;
  88. udelay(10);
  89. }
  90. return isr;
  91. }
  92. #endif /* POLLED_MODE */
  93. static void
  94. do_stop(struct keywest_iface* iface, int result)
  95. {
  96. write_reg(reg_control, KW_I2C_CTL_STOP);
  97. iface->state = state_stop;
  98. iface->result = result;
  99. }
  100. /* Main state machine for standard & standard sub mode */
  101. static void
  102. handle_interrupt(struct keywest_iface *iface, u8 isr)
  103. {
  104. int ack;
  105. if (isr == 0) {
  106. if (iface->state != state_stop) {
  107. pr_debug("KW: Timeout !\n");
  108. do_stop(iface, -EIO);
  109. }
  110. if (iface->state == state_stop) {
  111. ack = read_reg(reg_status);
  112. if (!(ack & KW_I2C_STAT_BUSY)) {
  113. iface->state = state_idle;
  114. write_reg(reg_ier, 0x00);
  115. #ifndef POLLED_MODE
  116. complete(&iface->complete);
  117. #endif /* POLLED_MODE */
  118. }
  119. }
  120. return;
  121. }
  122. if (isr & KW_I2C_IRQ_ADDR) {
  123. ack = read_reg(reg_status);
  124. if (iface->state != state_addr) {
  125. write_reg(reg_isr, KW_I2C_IRQ_ADDR);
  126. WRONG_STATE("KW_I2C_IRQ_ADDR");
  127. do_stop(iface, -EIO);
  128. return;
  129. }
  130. if ((ack & KW_I2C_STAT_LAST_AAK) == 0) {
  131. iface->state = state_stop;
  132. iface->result = -ENODEV;
  133. pr_debug("KW: NAK on address\n");
  134. } else {
  135. /* Handle rw "quick" mode */
  136. if (iface->datalen == 0) {
  137. do_stop(iface, 0);
  138. } else if (iface->read_write == I2C_SMBUS_READ) {
  139. iface->state = state_read;
  140. if (iface->datalen > 1)
  141. write_reg(reg_control, KW_I2C_CTL_AAK);
  142. } else {
  143. iface->state = state_write;
  144. write_reg(reg_data, *(iface->data++));
  145. iface->datalen--;
  146. }
  147. }
  148. write_reg(reg_isr, KW_I2C_IRQ_ADDR);
  149. }
  150. if (isr & KW_I2C_IRQ_DATA) {
  151. if (iface->state == state_read) {
  152. *(iface->data++) = read_reg(reg_data);
  153. write_reg(reg_isr, KW_I2C_IRQ_DATA);
  154. iface->datalen--;
  155. if (iface->datalen == 0)
  156. iface->state = state_stop;
  157. else if (iface->datalen == 1)
  158. write_reg(reg_control, 0);
  159. } else if (iface->state == state_write) {
  160. /* Check ack status */
  161. ack = read_reg(reg_status);
  162. if ((ack & KW_I2C_STAT_LAST_AAK) == 0) {
  163. pr_debug("KW: nack on data write (%x): %x\n",
  164. iface->data[-1], ack);
  165. do_stop(iface, -EIO);
  166. } else if (iface->datalen) {
  167. write_reg(reg_data, *(iface->data++));
  168. iface->datalen--;
  169. } else {
  170. write_reg(reg_control, KW_I2C_CTL_STOP);
  171. iface->state = state_stop;
  172. iface->result = 0;
  173. }
  174. write_reg(reg_isr, KW_I2C_IRQ_DATA);
  175. } else {
  176. write_reg(reg_isr, KW_I2C_IRQ_DATA);
  177. WRONG_STATE("KW_I2C_IRQ_DATA");
  178. if (iface->state != state_stop)
  179. do_stop(iface, -EIO);
  180. }
  181. }
  182. if (isr & KW_I2C_IRQ_STOP) {
  183. write_reg(reg_isr, KW_I2C_IRQ_STOP);
  184. if (iface->state != state_stop) {
  185. WRONG_STATE("KW_I2C_IRQ_STOP");
  186. iface->result = -EIO;
  187. }
  188. iface->state = state_idle;
  189. write_reg(reg_ier, 0x00);
  190. #ifndef POLLED_MODE
  191. complete(&iface->complete);
  192. #endif /* POLLED_MODE */
  193. }
  194. if (isr & KW_I2C_IRQ_START)
  195. write_reg(reg_isr, KW_I2C_IRQ_START);
  196. }
  197. #ifndef POLLED_MODE
  198. /* Interrupt handler */
  199. static irqreturn_t
  200. keywest_irq(int irq, void *dev_id, struct pt_regs *regs)
  201. {
  202. struct keywest_iface *iface = (struct keywest_iface *)dev_id;
  203. unsigned long flags;
  204. spin_lock_irqsave(&iface->lock, flags);
  205. del_timer(&iface->timeout_timer);
  206. handle_interrupt(iface, read_reg(reg_isr));
  207. if (iface->state != state_idle) {
  208. iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
  209. add_timer(&iface->timeout_timer);
  210. }
  211. spin_unlock_irqrestore(&iface->lock, flags);
  212. return IRQ_HANDLED;
  213. }
  214. static void
  215. keywest_timeout(unsigned long data)
  216. {
  217. struct keywest_iface *iface = (struct keywest_iface *)data;
  218. unsigned long flags;
  219. pr_debug("timeout !\n");
  220. spin_lock_irqsave(&iface->lock, flags);
  221. handle_interrupt(iface, read_reg(reg_isr));
  222. if (iface->state != state_idle) {
  223. iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
  224. add_timer(&iface->timeout_timer);
  225. }
  226. spin_unlock_irqrestore(&iface->lock, flags);
  227. }
  228. #endif /* POLLED_MODE */
  229. /*
  230. * SMBUS-type transfer entrypoint
  231. */
  232. static s32
  233. keywest_smbus_xfer( struct i2c_adapter* adap,
  234. u16 addr,
  235. unsigned short flags,
  236. char read_write,
  237. u8 command,
  238. int size,
  239. union i2c_smbus_data* data)
  240. {
  241. struct keywest_chan* chan = i2c_get_adapdata(adap);
  242. struct keywest_iface* iface = chan->iface;
  243. int len;
  244. u8* buffer;
  245. u16 cur_word;
  246. int rc = 0;
  247. if (iface->state == state_dead)
  248. return -ENXIO;
  249. /* Prepare datas & select mode */
  250. iface->cur_mode &= ~KW_I2C_MODE_MODE_MASK;
  251. switch (size) {
  252. case I2C_SMBUS_QUICK:
  253. len = 0;
  254. buffer = NULL;
  255. iface->cur_mode |= KW_I2C_MODE_STANDARD;
  256. break;
  257. case I2C_SMBUS_BYTE:
  258. len = 1;
  259. buffer = &data->byte;
  260. iface->cur_mode |= KW_I2C_MODE_STANDARD;
  261. break;
  262. case I2C_SMBUS_BYTE_DATA:
  263. len = 1;
  264. buffer = &data->byte;
  265. iface->cur_mode |= KW_I2C_MODE_STANDARDSUB;
  266. break;
  267. case I2C_SMBUS_WORD_DATA:
  268. len = 2;
  269. cur_word = cpu_to_le16(data->word);
  270. buffer = (u8 *)&cur_word;
  271. iface->cur_mode |= KW_I2C_MODE_STANDARDSUB;
  272. break;
  273. case I2C_SMBUS_BLOCK_DATA:
  274. len = data->block[0];
  275. buffer = &data->block[1];
  276. iface->cur_mode |= KW_I2C_MODE_STANDARDSUB;
  277. break;
  278. default:
  279. return -1;
  280. }
  281. /* Turn a standardsub read into a combined mode access */
  282. if (read_write == I2C_SMBUS_READ
  283. && (iface->cur_mode & KW_I2C_MODE_MODE_MASK) == KW_I2C_MODE_STANDARDSUB) {
  284. iface->cur_mode &= ~KW_I2C_MODE_MODE_MASK;
  285. iface->cur_mode |= KW_I2C_MODE_COMBINED;
  286. }
  287. /* Original driver had this limitation */
  288. if (len > 32)
  289. len = 32;
  290. if (pmac_low_i2c_lock(iface->node))
  291. return -ENXIO;
  292. pr_debug("chan: %d, addr: 0x%x, transfer len: %d, read: %d\n",
  293. chan->chan_no, addr, len, read_write == I2C_SMBUS_READ);
  294. iface->data = buffer;
  295. iface->datalen = len;
  296. iface->state = state_addr;
  297. iface->result = 0;
  298. iface->read_write = read_write;
  299. /* Setup channel & clear pending irqs */
  300. write_reg(reg_isr, read_reg(reg_isr));
  301. write_reg(reg_mode, iface->cur_mode | (chan->chan_no << 4));
  302. write_reg(reg_status, 0);
  303. /* Set up address and r/w bit */
  304. write_reg(reg_addr,
  305. (addr << 1) | ((read_write == I2C_SMBUS_READ) ? 0x01 : 0x00));
  306. /* Set up the sub address */
  307. if ((iface->cur_mode & KW_I2C_MODE_MODE_MASK) == KW_I2C_MODE_STANDARDSUB
  308. || (iface->cur_mode & KW_I2C_MODE_MODE_MASK) == KW_I2C_MODE_COMBINED)
  309. write_reg(reg_subaddr, command);
  310. #ifndef POLLED_MODE
  311. /* Arm timeout */
  312. iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
  313. add_timer(&iface->timeout_timer);
  314. #endif
  315. /* Start sending address & enable interrupt*/
  316. write_reg(reg_control, KW_I2C_CTL_XADDR);
  317. write_reg(reg_ier, KW_I2C_IRQ_MASK);
  318. #ifdef POLLED_MODE
  319. pr_debug("using polled mode...\n");
  320. /* State machine, to turn into an interrupt handler */
  321. while(iface->state != state_idle) {
  322. unsigned long flags;
  323. u8 isr = wait_interrupt(iface);
  324. spin_lock_irqsave(&iface->lock, flags);
  325. handle_interrupt(iface, isr);
  326. spin_unlock_irqrestore(&iface->lock, flags);
  327. }
  328. #else /* POLLED_MODE */
  329. pr_debug("using interrupt mode...\n");
  330. wait_for_completion(&iface->complete);
  331. #endif /* POLLED_MODE */
  332. rc = iface->result;
  333. pr_debug("transfer done, result: %d\n", rc);
  334. if (rc == 0 && size == I2C_SMBUS_WORD_DATA && read_write == I2C_SMBUS_READ)
  335. data->word = le16_to_cpu(cur_word);
  336. /* Release sem */
  337. pmac_low_i2c_unlock(iface->node);
  338. return rc;
  339. }
  340. /*
  341. * Generic i2c master transfer entrypoint
  342. */
  343. static int
  344. keywest_xfer( struct i2c_adapter *adap,
  345. struct i2c_msg *msgs,
  346. int num)
  347. {
  348. struct keywest_chan* chan = i2c_get_adapdata(adap);
  349. struct keywest_iface* iface = chan->iface;
  350. struct i2c_msg *pmsg;
  351. int i, completed;
  352. int rc = 0;
  353. if (iface->state == state_dead)
  354. return -ENXIO;
  355. if (pmac_low_i2c_lock(iface->node))
  356. return -ENXIO;
  357. /* Set adapter to standard mode */
  358. iface->cur_mode &= ~KW_I2C_MODE_MODE_MASK;
  359. iface->cur_mode |= KW_I2C_MODE_STANDARD;
  360. completed = 0;
  361. for (i = 0; rc >= 0 && i < num;) {
  362. u8 addr;
  363. pmsg = &msgs[i++];
  364. addr = pmsg->addr;
  365. if (pmsg->flags & I2C_M_TEN) {
  366. printk(KERN_ERR "i2c-keywest: 10 bits addr not supported !\n");
  367. rc = -EINVAL;
  368. break;
  369. }
  370. pr_debug("xfer: chan: %d, doing %s %d bytes to 0x%02x - %d of %d messages\n",
  371. chan->chan_no,
  372. pmsg->flags & I2C_M_RD ? "read" : "write",
  373. pmsg->len, addr, i, num);
  374. /* Setup channel & clear pending irqs */
  375. write_reg(reg_mode, iface->cur_mode | (chan->chan_no << 4));
  376. write_reg(reg_isr, read_reg(reg_isr));
  377. write_reg(reg_status, 0);
  378. iface->data = pmsg->buf;
  379. iface->datalen = pmsg->len;
  380. iface->state = state_addr;
  381. iface->result = 0;
  382. if (pmsg->flags & I2C_M_RD)
  383. iface->read_write = I2C_SMBUS_READ;
  384. else
  385. iface->read_write = I2C_SMBUS_WRITE;
  386. /* Set up address and r/w bit */
  387. if (pmsg->flags & I2C_M_REV_DIR_ADDR)
  388. addr ^= 1;
  389. write_reg(reg_addr,
  390. (addr << 1) |
  391. ((iface->read_write == I2C_SMBUS_READ) ? 0x01 : 0x00));
  392. #ifndef POLLED_MODE
  393. /* Arm timeout */
  394. iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
  395. add_timer(&iface->timeout_timer);
  396. #endif
  397. /* Start sending address & enable interrupt*/
  398. write_reg(reg_ier, KW_I2C_IRQ_MASK);
  399. write_reg(reg_control, KW_I2C_CTL_XADDR);
  400. #ifdef POLLED_MODE
  401. pr_debug("using polled mode...\n");
  402. /* State machine, to turn into an interrupt handler */
  403. while(iface->state != state_idle) {
  404. u8 isr = wait_interrupt(iface);
  405. handle_interrupt(iface, isr);
  406. }
  407. #else /* POLLED_MODE */
  408. pr_debug("using interrupt mode...\n");
  409. wait_for_completion(&iface->complete);
  410. #endif /* POLLED_MODE */
  411. rc = iface->result;
  412. if (rc == 0)
  413. completed++;
  414. pr_debug("transfer done, result: %d\n", rc);
  415. }
  416. /* Release sem */
  417. pmac_low_i2c_unlock(iface->node);
  418. return completed;
  419. }
  420. static u32
  421. keywest_func(struct i2c_adapter * adapter)
  422. {
  423. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  424. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  425. I2C_FUNC_SMBUS_BLOCK_DATA;
  426. }
  427. /* For now, we only handle combined mode (smbus) */
  428. static struct i2c_algorithm keywest_algorithm = {
  429. .smbus_xfer = keywest_smbus_xfer,
  430. .master_xfer = keywest_xfer,
  431. .functionality = keywest_func,
  432. };
  433. static int
  434. create_iface(struct device_node *np, struct device *dev)
  435. {
  436. unsigned long steps;
  437. unsigned bsteps, tsize, i, nchan, addroffset;
  438. struct keywest_iface* iface;
  439. u32 *psteps, *prate;
  440. int rc;
  441. if (np->n_intrs < 1 || np->n_addrs < 1) {
  442. printk(KERN_ERR "%s: Missing interrupt or address !\n",
  443. np->full_name);
  444. return -ENODEV;
  445. }
  446. if (pmac_low_i2c_lock(np))
  447. return -ENODEV;
  448. psteps = (u32 *)get_property(np, "AAPL,address-step", NULL);
  449. steps = psteps ? (*psteps) : 0x10;
  450. /* Hrm... maybe we can be smarter here */
  451. for (bsteps = 0; (steps & 0x01) == 0; bsteps++)
  452. steps >>= 1;
  453. if (np->parent->name[0] == 'u') {
  454. nchan = 2;
  455. addroffset = 3;
  456. } else {
  457. addroffset = 0;
  458. nchan = 1;
  459. }
  460. tsize = sizeof(struct keywest_iface) +
  461. (sizeof(struct keywest_chan) + 4) * nchan;
  462. iface = (struct keywest_iface *) kmalloc(tsize, GFP_KERNEL);
  463. if (iface == NULL) {
  464. printk(KERN_ERR "i2c-keywest: can't allocate inteface !\n");
  465. pmac_low_i2c_unlock(np);
  466. return -ENOMEM;
  467. }
  468. memset(iface, 0, tsize);
  469. spin_lock_init(&iface->lock);
  470. init_completion(&iface->complete);
  471. iface->node = of_node_get(np);
  472. iface->bsteps = bsteps;
  473. iface->chan_count = nchan;
  474. iface->state = state_idle;
  475. iface->irq = np->intrs[0].line;
  476. iface->channels = (struct keywest_chan *)
  477. (((unsigned long)(iface + 1) + 3UL) & ~3UL);
  478. iface->base = ioremap(np->addrs[0].address + addroffset,
  479. np->addrs[0].size);
  480. if (!iface->base) {
  481. printk(KERN_ERR "i2c-keywest: can't map inteface !\n");
  482. kfree(iface);
  483. pmac_low_i2c_unlock(np);
  484. return -ENOMEM;
  485. }
  486. #ifndef POLLED_MODE
  487. init_timer(&iface->timeout_timer);
  488. iface->timeout_timer.function = keywest_timeout;
  489. iface->timeout_timer.data = (unsigned long)iface;
  490. #endif
  491. /* Select interface rate */
  492. iface->cur_mode = KW_I2C_MODE_100KHZ;
  493. prate = (u32 *)get_property(np, "AAPL,i2c-rate", NULL);
  494. if (prate) switch(*prate) {
  495. case 100:
  496. iface->cur_mode = KW_I2C_MODE_100KHZ;
  497. break;
  498. case 50:
  499. iface->cur_mode = KW_I2C_MODE_50KHZ;
  500. break;
  501. case 25:
  502. iface->cur_mode = KW_I2C_MODE_25KHZ;
  503. break;
  504. default:
  505. printk(KERN_WARNING "i2c-keywest: unknown rate %ldKhz, using 100KHz\n",
  506. (long)*prate);
  507. }
  508. /* Select standard mode by default */
  509. iface->cur_mode |= KW_I2C_MODE_STANDARD;
  510. /* Write mode */
  511. write_reg(reg_mode, iface->cur_mode);
  512. /* Switch interrupts off & clear them*/
  513. write_reg(reg_ier, 0x00);
  514. write_reg(reg_isr, KW_I2C_IRQ_MASK);
  515. #ifndef POLLED_MODE
  516. /* Request chip interrupt */
  517. rc = request_irq(iface->irq, keywest_irq, SA_INTERRUPT, "keywest i2c", iface);
  518. if (rc) {
  519. printk(KERN_ERR "i2c-keywest: can't get IRQ %d !\n", iface->irq);
  520. iounmap(iface->base);
  521. kfree(iface);
  522. pmac_low_i2c_unlock(np);
  523. return -ENODEV;
  524. }
  525. #endif /* POLLED_MODE */
  526. pmac_low_i2c_unlock(np);
  527. dev_set_drvdata(dev, iface);
  528. for (i=0; i<nchan; i++) {
  529. struct keywest_chan* chan = &iface->channels[i];
  530. u8 addr;
  531. sprintf(chan->adapter.name, "%s %d", np->parent->name, i);
  532. chan->iface = iface;
  533. chan->chan_no = i;
  534. chan->adapter.algo = &keywest_algorithm;
  535. chan->adapter.algo_data = NULL;
  536. chan->adapter.client_register = NULL;
  537. chan->adapter.client_unregister = NULL;
  538. i2c_set_adapdata(&chan->adapter, chan);
  539. chan->adapter.dev.parent = dev;
  540. rc = i2c_add_adapter(&chan->adapter);
  541. if (rc) {
  542. printk("i2c-keywest.c: Adapter %s registration failed\n",
  543. chan->adapter.name);
  544. i2c_set_adapdata(&chan->adapter, NULL);
  545. }
  546. }
  547. printk(KERN_INFO "Found KeyWest i2c on \"%s\", %d channel%s, stepping: %d bits\n",
  548. np->parent->name, nchan, nchan > 1 ? "s" : "", bsteps);
  549. return 0;
  550. }
  551. static int
  552. dispose_iface(struct device *dev)
  553. {
  554. struct keywest_iface *iface = dev_get_drvdata(dev);
  555. int i, rc;
  556. /* Make sure we stop all activity */
  557. if (pmac_low_i2c_lock(iface->node))
  558. return -ENODEV;
  559. #ifndef POLLED_MODE
  560. spin_lock_irq(&iface->lock);
  561. while (iface->state != state_idle) {
  562. spin_unlock_irq(&iface->lock);
  563. msleep(100);
  564. spin_lock_irq(&iface->lock);
  565. }
  566. #endif /* POLLED_MODE */
  567. iface->state = state_dead;
  568. #ifndef POLLED_MODE
  569. spin_unlock_irq(&iface->lock);
  570. free_irq(iface->irq, iface);
  571. #endif /* POLLED_MODE */
  572. pmac_low_i2c_unlock(iface->node);
  573. /* Release all channels */
  574. for (i=0; i<iface->chan_count; i++) {
  575. struct keywest_chan* chan = &iface->channels[i];
  576. if (i2c_get_adapdata(&chan->adapter) == NULL)
  577. continue;
  578. rc = i2c_del_adapter(&chan->adapter);
  579. i2c_set_adapdata(&chan->adapter, NULL);
  580. /* We aren't that prepared to deal with this... */
  581. if (rc)
  582. printk("i2c-keywest.c: i2c_del_adapter failed, that's bad !\n");
  583. }
  584. iounmap(iface->base);
  585. dev_set_drvdata(dev, NULL);
  586. of_node_put(iface->node);
  587. kfree(iface);
  588. return 0;
  589. }
  590. static int
  591. create_iface_macio(struct macio_dev* dev, const struct of_device_id *match)
  592. {
  593. return create_iface(dev->ofdev.node, &dev->ofdev.dev);
  594. }
  595. static int
  596. dispose_iface_macio(struct macio_dev* dev)
  597. {
  598. return dispose_iface(&dev->ofdev.dev);
  599. }
  600. static int
  601. create_iface_of_platform(struct of_device* dev, const struct of_device_id *match)
  602. {
  603. return create_iface(dev->node, &dev->dev);
  604. }
  605. static int
  606. dispose_iface_of_platform(struct of_device* dev)
  607. {
  608. return dispose_iface(&dev->dev);
  609. }
  610. static struct of_device_id i2c_keywest_match[] =
  611. {
  612. {
  613. .type = "i2c",
  614. .compatible = "keywest"
  615. },
  616. {},
  617. };
  618. static struct macio_driver i2c_keywest_macio_driver =
  619. {
  620. .name = "i2c-keywest",
  621. .match_table = i2c_keywest_match,
  622. .probe = create_iface_macio,
  623. .remove = dispose_iface_macio
  624. };
  625. static struct of_platform_driver i2c_keywest_of_platform_driver =
  626. {
  627. .name = "i2c-keywest",
  628. .match_table = i2c_keywest_match,
  629. .probe = create_iface_of_platform,
  630. .remove = dispose_iface_of_platform
  631. };
  632. static int __init
  633. i2c_keywest_init(void)
  634. {
  635. of_register_driver(&i2c_keywest_of_platform_driver);
  636. macio_register_driver(&i2c_keywest_macio_driver);
  637. return 0;
  638. }
  639. static void __exit
  640. i2c_keywest_cleanup(void)
  641. {
  642. of_unregister_driver(&i2c_keywest_of_platform_driver);
  643. macio_unregister_driver(&i2c_keywest_macio_driver);
  644. }
  645. module_init(i2c_keywest_init);
  646. module_exit(i2c_keywest_cleanup);