i2c-keywest.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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. static int probe;
  73. MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
  74. MODULE_DESCRIPTION("I2C driver for Apple's Keywest");
  75. MODULE_LICENSE("GPL");
  76. module_param(probe, bool, 0);
  77. #ifdef POLLED_MODE
  78. /* Don't schedule, the g5 fan controller is too
  79. * timing sensitive
  80. */
  81. static u8
  82. wait_interrupt(struct keywest_iface* iface)
  83. {
  84. int i;
  85. u8 isr;
  86. for (i = 0; i < 200000; i++) {
  87. isr = read_reg(reg_isr) & KW_I2C_IRQ_MASK;
  88. if (isr != 0)
  89. return isr;
  90. udelay(10);
  91. }
  92. return isr;
  93. }
  94. #endif /* POLLED_MODE */
  95. static void
  96. do_stop(struct keywest_iface* iface, int result)
  97. {
  98. write_reg(reg_control, KW_I2C_CTL_STOP);
  99. iface->state = state_stop;
  100. iface->result = result;
  101. }
  102. /* Main state machine for standard & standard sub mode */
  103. static void
  104. handle_interrupt(struct keywest_iface *iface, u8 isr)
  105. {
  106. int ack;
  107. if (isr == 0) {
  108. if (iface->state != state_stop) {
  109. pr_debug("KW: Timeout !\n");
  110. do_stop(iface, -EIO);
  111. }
  112. if (iface->state == state_stop) {
  113. ack = read_reg(reg_status);
  114. if (!(ack & KW_I2C_STAT_BUSY)) {
  115. iface->state = state_idle;
  116. write_reg(reg_ier, 0x00);
  117. #ifndef POLLED_MODE
  118. complete(&iface->complete);
  119. #endif /* POLLED_MODE */
  120. }
  121. }
  122. return;
  123. }
  124. if (isr & KW_I2C_IRQ_ADDR) {
  125. ack = read_reg(reg_status);
  126. if (iface->state != state_addr) {
  127. write_reg(reg_isr, KW_I2C_IRQ_ADDR);
  128. WRONG_STATE("KW_I2C_IRQ_ADDR");
  129. do_stop(iface, -EIO);
  130. return;
  131. }
  132. if ((ack & KW_I2C_STAT_LAST_AAK) == 0) {
  133. iface->state = state_stop;
  134. iface->result = -ENODEV;
  135. pr_debug("KW: NAK on address\n");
  136. } else {
  137. /* Handle rw "quick" mode */
  138. if (iface->datalen == 0) {
  139. do_stop(iface, 0);
  140. } else if (iface->read_write == I2C_SMBUS_READ) {
  141. iface->state = state_read;
  142. if (iface->datalen > 1)
  143. write_reg(reg_control, KW_I2C_CTL_AAK);
  144. } else {
  145. iface->state = state_write;
  146. write_reg(reg_data, *(iface->data++));
  147. iface->datalen--;
  148. }
  149. }
  150. write_reg(reg_isr, KW_I2C_IRQ_ADDR);
  151. }
  152. if (isr & KW_I2C_IRQ_DATA) {
  153. if (iface->state == state_read) {
  154. *(iface->data++) = read_reg(reg_data);
  155. write_reg(reg_isr, KW_I2C_IRQ_DATA);
  156. iface->datalen--;
  157. if (iface->datalen == 0)
  158. iface->state = state_stop;
  159. else if (iface->datalen == 1)
  160. write_reg(reg_control, 0);
  161. } else if (iface->state == state_write) {
  162. /* Check ack status */
  163. ack = read_reg(reg_status);
  164. if ((ack & KW_I2C_STAT_LAST_AAK) == 0) {
  165. pr_debug("KW: nack on data write (%x): %x\n",
  166. iface->data[-1], ack);
  167. do_stop(iface, -EIO);
  168. } else if (iface->datalen) {
  169. write_reg(reg_data, *(iface->data++));
  170. iface->datalen--;
  171. } else {
  172. write_reg(reg_control, KW_I2C_CTL_STOP);
  173. iface->state = state_stop;
  174. iface->result = 0;
  175. }
  176. write_reg(reg_isr, KW_I2C_IRQ_DATA);
  177. } else {
  178. write_reg(reg_isr, KW_I2C_IRQ_DATA);
  179. WRONG_STATE("KW_I2C_IRQ_DATA");
  180. if (iface->state != state_stop)
  181. do_stop(iface, -EIO);
  182. }
  183. }
  184. if (isr & KW_I2C_IRQ_STOP) {
  185. write_reg(reg_isr, KW_I2C_IRQ_STOP);
  186. if (iface->state != state_stop) {
  187. WRONG_STATE("KW_I2C_IRQ_STOP");
  188. iface->result = -EIO;
  189. }
  190. iface->state = state_idle;
  191. write_reg(reg_ier, 0x00);
  192. #ifndef POLLED_MODE
  193. complete(&iface->complete);
  194. #endif /* POLLED_MODE */
  195. }
  196. if (isr & KW_I2C_IRQ_START)
  197. write_reg(reg_isr, KW_I2C_IRQ_START);
  198. }
  199. #ifndef POLLED_MODE
  200. /* Interrupt handler */
  201. static irqreturn_t
  202. keywest_irq(int irq, void *dev_id, struct pt_regs *regs)
  203. {
  204. struct keywest_iface *iface = (struct keywest_iface *)dev_id;
  205. unsigned long flags;
  206. spin_lock_irqsave(&iface->lock, flags);
  207. del_timer(&iface->timeout_timer);
  208. handle_interrupt(iface, read_reg(reg_isr));
  209. if (iface->state != state_idle) {
  210. iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
  211. add_timer(&iface->timeout_timer);
  212. }
  213. spin_unlock_irqrestore(&iface->lock, flags);
  214. return IRQ_HANDLED;
  215. }
  216. static void
  217. keywest_timeout(unsigned long data)
  218. {
  219. struct keywest_iface *iface = (struct keywest_iface *)data;
  220. unsigned long flags;
  221. pr_debug("timeout !\n");
  222. spin_lock_irqsave(&iface->lock, flags);
  223. handle_interrupt(iface, read_reg(reg_isr));
  224. if (iface->state != state_idle) {
  225. iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
  226. add_timer(&iface->timeout_timer);
  227. }
  228. spin_unlock_irqrestore(&iface->lock, flags);
  229. }
  230. #endif /* POLLED_MODE */
  231. /*
  232. * SMBUS-type transfer entrypoint
  233. */
  234. static s32
  235. keywest_smbus_xfer( struct i2c_adapter* adap,
  236. u16 addr,
  237. unsigned short flags,
  238. char read_write,
  239. u8 command,
  240. int size,
  241. union i2c_smbus_data* data)
  242. {
  243. struct keywest_chan* chan = i2c_get_adapdata(adap);
  244. struct keywest_iface* iface = chan->iface;
  245. int len;
  246. u8* buffer;
  247. u16 cur_word;
  248. int rc = 0;
  249. if (iface->state == state_dead)
  250. return -ENXIO;
  251. /* Prepare datas & select mode */
  252. iface->cur_mode &= ~KW_I2C_MODE_MODE_MASK;
  253. switch (size) {
  254. case I2C_SMBUS_QUICK:
  255. len = 0;
  256. buffer = NULL;
  257. iface->cur_mode |= KW_I2C_MODE_STANDARD;
  258. break;
  259. case I2C_SMBUS_BYTE:
  260. len = 1;
  261. buffer = &data->byte;
  262. iface->cur_mode |= KW_I2C_MODE_STANDARD;
  263. break;
  264. case I2C_SMBUS_BYTE_DATA:
  265. len = 1;
  266. buffer = &data->byte;
  267. iface->cur_mode |= KW_I2C_MODE_STANDARDSUB;
  268. break;
  269. case I2C_SMBUS_WORD_DATA:
  270. len = 2;
  271. cur_word = cpu_to_le16(data->word);
  272. buffer = (u8 *)&cur_word;
  273. iface->cur_mode |= KW_I2C_MODE_STANDARDSUB;
  274. break;
  275. case I2C_SMBUS_BLOCK_DATA:
  276. len = data->block[0];
  277. buffer = &data->block[1];
  278. iface->cur_mode |= KW_I2C_MODE_STANDARDSUB;
  279. break;
  280. default:
  281. return -1;
  282. }
  283. /* Turn a standardsub read into a combined mode access */
  284. if (read_write == I2C_SMBUS_READ
  285. && (iface->cur_mode & KW_I2C_MODE_MODE_MASK) == KW_I2C_MODE_STANDARDSUB) {
  286. iface->cur_mode &= ~KW_I2C_MODE_MODE_MASK;
  287. iface->cur_mode |= KW_I2C_MODE_COMBINED;
  288. }
  289. /* Original driver had this limitation */
  290. if (len > 32)
  291. len = 32;
  292. if (pmac_low_i2c_lock(iface->node))
  293. return -ENXIO;
  294. pr_debug("chan: %d, addr: 0x%x, transfer len: %d, read: %d\n",
  295. chan->chan_no, addr, len, read_write == I2C_SMBUS_READ);
  296. iface->data = buffer;
  297. iface->datalen = len;
  298. iface->state = state_addr;
  299. iface->result = 0;
  300. iface->read_write = read_write;
  301. /* Setup channel & clear pending irqs */
  302. write_reg(reg_isr, read_reg(reg_isr));
  303. write_reg(reg_mode, iface->cur_mode | (chan->chan_no << 4));
  304. write_reg(reg_status, 0);
  305. /* Set up address and r/w bit */
  306. write_reg(reg_addr,
  307. (addr << 1) | ((read_write == I2C_SMBUS_READ) ? 0x01 : 0x00));
  308. /* Set up the sub address */
  309. if ((iface->cur_mode & KW_I2C_MODE_MODE_MASK) == KW_I2C_MODE_STANDARDSUB
  310. || (iface->cur_mode & KW_I2C_MODE_MODE_MASK) == KW_I2C_MODE_COMBINED)
  311. write_reg(reg_subaddr, command);
  312. #ifndef POLLED_MODE
  313. /* Arm timeout */
  314. iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
  315. add_timer(&iface->timeout_timer);
  316. #endif
  317. /* Start sending address & enable interrupt*/
  318. write_reg(reg_control, KW_I2C_CTL_XADDR);
  319. write_reg(reg_ier, KW_I2C_IRQ_MASK);
  320. #ifdef POLLED_MODE
  321. pr_debug("using polled mode...\n");
  322. /* State machine, to turn into an interrupt handler */
  323. while(iface->state != state_idle) {
  324. unsigned long flags;
  325. u8 isr = wait_interrupt(iface);
  326. spin_lock_irqsave(&iface->lock, flags);
  327. handle_interrupt(iface, isr);
  328. spin_unlock_irqrestore(&iface->lock, flags);
  329. }
  330. #else /* POLLED_MODE */
  331. pr_debug("using interrupt mode...\n");
  332. wait_for_completion(&iface->complete);
  333. #endif /* POLLED_MODE */
  334. rc = iface->result;
  335. pr_debug("transfer done, result: %d\n", rc);
  336. if (rc == 0 && size == I2C_SMBUS_WORD_DATA && read_write == I2C_SMBUS_READ)
  337. data->word = le16_to_cpu(cur_word);
  338. /* Release sem */
  339. pmac_low_i2c_unlock(iface->node);
  340. return rc;
  341. }
  342. /*
  343. * Generic i2c master transfer entrypoint
  344. */
  345. static int
  346. keywest_xfer( struct i2c_adapter *adap,
  347. struct i2c_msg *msgs,
  348. int num)
  349. {
  350. struct keywest_chan* chan = i2c_get_adapdata(adap);
  351. struct keywest_iface* iface = chan->iface;
  352. struct i2c_msg *pmsg;
  353. int i, completed;
  354. int rc = 0;
  355. if (iface->state == state_dead)
  356. return -ENXIO;
  357. if (pmac_low_i2c_lock(iface->node))
  358. return -ENXIO;
  359. /* Set adapter to standard mode */
  360. iface->cur_mode &= ~KW_I2C_MODE_MODE_MASK;
  361. iface->cur_mode |= KW_I2C_MODE_STANDARD;
  362. completed = 0;
  363. for (i = 0; rc >= 0 && i < num;) {
  364. u8 addr;
  365. pmsg = &msgs[i++];
  366. addr = pmsg->addr;
  367. if (pmsg->flags & I2C_M_TEN) {
  368. printk(KERN_ERR "i2c-keywest: 10 bits addr not supported !\n");
  369. rc = -EINVAL;
  370. break;
  371. }
  372. pr_debug("xfer: chan: %d, doing %s %d bytes to 0x%02x - %d of %d messages\n",
  373. chan->chan_no,
  374. pmsg->flags & I2C_M_RD ? "read" : "write",
  375. pmsg->len, addr, i, num);
  376. /* Setup channel & clear pending irqs */
  377. write_reg(reg_mode, iface->cur_mode | (chan->chan_no << 4));
  378. write_reg(reg_isr, read_reg(reg_isr));
  379. write_reg(reg_status, 0);
  380. iface->data = pmsg->buf;
  381. iface->datalen = pmsg->len;
  382. iface->state = state_addr;
  383. iface->result = 0;
  384. if (pmsg->flags & I2C_M_RD)
  385. iface->read_write = I2C_SMBUS_READ;
  386. else
  387. iface->read_write = I2C_SMBUS_WRITE;
  388. /* Set up address and r/w bit */
  389. if (pmsg->flags & I2C_M_REV_DIR_ADDR)
  390. addr ^= 1;
  391. write_reg(reg_addr,
  392. (addr << 1) |
  393. ((iface->read_write == I2C_SMBUS_READ) ? 0x01 : 0x00));
  394. #ifndef POLLED_MODE
  395. /* Arm timeout */
  396. iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
  397. add_timer(&iface->timeout_timer);
  398. #endif
  399. /* Start sending address & enable interrupt*/
  400. write_reg(reg_ier, KW_I2C_IRQ_MASK);
  401. write_reg(reg_control, KW_I2C_CTL_XADDR);
  402. #ifdef POLLED_MODE
  403. pr_debug("using polled mode...\n");
  404. /* State machine, to turn into an interrupt handler */
  405. while(iface->state != state_idle) {
  406. u8 isr = wait_interrupt(iface);
  407. handle_interrupt(iface, isr);
  408. }
  409. #else /* POLLED_MODE */
  410. pr_debug("using interrupt mode...\n");
  411. wait_for_completion(&iface->complete);
  412. #endif /* POLLED_MODE */
  413. rc = iface->result;
  414. if (rc == 0)
  415. completed++;
  416. pr_debug("transfer done, result: %d\n", rc);
  417. }
  418. /* Release sem */
  419. pmac_low_i2c_unlock(iface->node);
  420. return completed;
  421. }
  422. static u32
  423. keywest_func(struct i2c_adapter * adapter)
  424. {
  425. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  426. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  427. I2C_FUNC_SMBUS_BLOCK_DATA;
  428. }
  429. /* For now, we only handle combined mode (smbus) */
  430. static struct i2c_algorithm keywest_algorithm = {
  431. .name = "Keywest i2c",
  432. .id = I2C_ALGO_SMBUS,
  433. .smbus_xfer = keywest_smbus_xfer,
  434. .master_xfer = keywest_xfer,
  435. .functionality = keywest_func,
  436. };
  437. static int
  438. create_iface(struct device_node *np, struct device *dev)
  439. {
  440. unsigned long steps;
  441. unsigned bsteps, tsize, i, nchan, addroffset;
  442. struct keywest_iface* iface;
  443. u32 *psteps, *prate;
  444. int rc;
  445. if (np->n_intrs < 1 || np->n_addrs < 1) {
  446. printk(KERN_ERR "%s: Missing interrupt or address !\n",
  447. np->full_name);
  448. return -ENODEV;
  449. }
  450. if (pmac_low_i2c_lock(np))
  451. return -ENODEV;
  452. psteps = (u32 *)get_property(np, "AAPL,address-step", NULL);
  453. steps = psteps ? (*psteps) : 0x10;
  454. /* Hrm... maybe we can be smarter here */
  455. for (bsteps = 0; (steps & 0x01) == 0; bsteps++)
  456. steps >>= 1;
  457. if (np->parent->name[0] == 'u') {
  458. nchan = 2;
  459. addroffset = 3;
  460. } else {
  461. addroffset = 0;
  462. nchan = 1;
  463. }
  464. tsize = sizeof(struct keywest_iface) +
  465. (sizeof(struct keywest_chan) + 4) * nchan;
  466. iface = (struct keywest_iface *) kmalloc(tsize, GFP_KERNEL);
  467. if (iface == NULL) {
  468. printk(KERN_ERR "i2c-keywest: can't allocate inteface !\n");
  469. pmac_low_i2c_unlock(np);
  470. return -ENOMEM;
  471. }
  472. memset(iface, 0, tsize);
  473. spin_lock_init(&iface->lock);
  474. init_completion(&iface->complete);
  475. iface->node = of_node_get(np);
  476. iface->bsteps = bsteps;
  477. iface->chan_count = nchan;
  478. iface->state = state_idle;
  479. iface->irq = np->intrs[0].line;
  480. iface->channels = (struct keywest_chan *)
  481. (((unsigned long)(iface + 1) + 3UL) & ~3UL);
  482. iface->base = ioremap(np->addrs[0].address + addroffset,
  483. np->addrs[0].size);
  484. if (!iface->base) {
  485. printk(KERN_ERR "i2c-keywest: can't map inteface !\n");
  486. kfree(iface);
  487. pmac_low_i2c_unlock(np);
  488. return -ENOMEM;
  489. }
  490. #ifndef POLLED_MODE
  491. init_timer(&iface->timeout_timer);
  492. iface->timeout_timer.function = keywest_timeout;
  493. iface->timeout_timer.data = (unsigned long)iface;
  494. #endif
  495. /* Select interface rate */
  496. iface->cur_mode = KW_I2C_MODE_100KHZ;
  497. prate = (u32 *)get_property(np, "AAPL,i2c-rate", NULL);
  498. if (prate) switch(*prate) {
  499. case 100:
  500. iface->cur_mode = KW_I2C_MODE_100KHZ;
  501. break;
  502. case 50:
  503. iface->cur_mode = KW_I2C_MODE_50KHZ;
  504. break;
  505. case 25:
  506. iface->cur_mode = KW_I2C_MODE_25KHZ;
  507. break;
  508. default:
  509. printk(KERN_WARNING "i2c-keywest: unknown rate %ldKhz, using 100KHz\n",
  510. (long)*prate);
  511. }
  512. /* Select standard mode by default */
  513. iface->cur_mode |= KW_I2C_MODE_STANDARD;
  514. /* Write mode */
  515. write_reg(reg_mode, iface->cur_mode);
  516. /* Switch interrupts off & clear them*/
  517. write_reg(reg_ier, 0x00);
  518. write_reg(reg_isr, KW_I2C_IRQ_MASK);
  519. #ifndef POLLED_MODE
  520. /* Request chip interrupt */
  521. rc = request_irq(iface->irq, keywest_irq, SA_INTERRUPT, "keywest i2c", iface);
  522. if (rc) {
  523. printk(KERN_ERR "i2c-keywest: can't get IRQ %d !\n", iface->irq);
  524. iounmap(iface->base);
  525. kfree(iface);
  526. pmac_low_i2c_unlock(np);
  527. return -ENODEV;
  528. }
  529. #endif /* POLLED_MODE */
  530. pmac_low_i2c_unlock(np);
  531. dev_set_drvdata(dev, iface);
  532. for (i=0; i<nchan; i++) {
  533. struct keywest_chan* chan = &iface->channels[i];
  534. u8 addr;
  535. sprintf(chan->adapter.name, "%s %d", np->parent->name, i);
  536. chan->iface = iface;
  537. chan->chan_no = i;
  538. chan->adapter.id = I2C_ALGO_SMBUS;
  539. chan->adapter.algo = &keywest_algorithm;
  540. chan->adapter.algo_data = NULL;
  541. chan->adapter.client_register = NULL;
  542. chan->adapter.client_unregister = NULL;
  543. i2c_set_adapdata(&chan->adapter, chan);
  544. chan->adapter.dev.parent = dev;
  545. rc = i2c_add_adapter(&chan->adapter);
  546. if (rc) {
  547. printk("i2c-keywest.c: Adapter %s registration failed\n",
  548. chan->adapter.name);
  549. i2c_set_adapdata(&chan->adapter, NULL);
  550. }
  551. if (probe) {
  552. printk("Probe: ");
  553. for (addr = 0x00; addr <= 0x7f; addr++) {
  554. if (i2c_smbus_xfer(&chan->adapter,addr,
  555. 0,0,0,I2C_SMBUS_QUICK,NULL) >= 0)
  556. printk("%02x ", addr);
  557. }
  558. printk("\n");
  559. }
  560. }
  561. printk(KERN_INFO "Found KeyWest i2c on \"%s\", %d channel%s, stepping: %d bits\n",
  562. np->parent->name, nchan, nchan > 1 ? "s" : "", bsteps);
  563. return 0;
  564. }
  565. static int
  566. dispose_iface(struct device *dev)
  567. {
  568. struct keywest_iface *iface = dev_get_drvdata(dev);
  569. int i, rc;
  570. /* Make sure we stop all activity */
  571. if (pmac_low_i2c_lock(iface->node))
  572. return -ENODEV;
  573. #ifndef POLLED_MODE
  574. spin_lock_irq(&iface->lock);
  575. while (iface->state != state_idle) {
  576. spin_unlock_irq(&iface->lock);
  577. msleep(100);
  578. spin_lock_irq(&iface->lock);
  579. }
  580. #endif /* POLLED_MODE */
  581. iface->state = state_dead;
  582. #ifndef POLLED_MODE
  583. spin_unlock_irq(&iface->lock);
  584. free_irq(iface->irq, iface);
  585. #endif /* POLLED_MODE */
  586. pmac_low_i2c_unlock(iface->node);
  587. /* Release all channels */
  588. for (i=0; i<iface->chan_count; i++) {
  589. struct keywest_chan* chan = &iface->channels[i];
  590. if (i2c_get_adapdata(&chan->adapter) == NULL)
  591. continue;
  592. rc = i2c_del_adapter(&chan->adapter);
  593. i2c_set_adapdata(&chan->adapter, NULL);
  594. /* We aren't that prepared to deal with this... */
  595. if (rc)
  596. printk("i2c-keywest.c: i2c_del_adapter failed, that's bad !\n");
  597. }
  598. iounmap(iface->base);
  599. dev_set_drvdata(dev, NULL);
  600. of_node_put(iface->node);
  601. kfree(iface);
  602. return 0;
  603. }
  604. static int
  605. create_iface_macio(struct macio_dev* dev, const struct of_match *match)
  606. {
  607. return create_iface(dev->ofdev.node, &dev->ofdev.dev);
  608. }
  609. static int
  610. dispose_iface_macio(struct macio_dev* dev)
  611. {
  612. return dispose_iface(&dev->ofdev.dev);
  613. }
  614. static int
  615. create_iface_of_platform(struct of_device* dev, const struct of_match *match)
  616. {
  617. return create_iface(dev->node, &dev->dev);
  618. }
  619. static int
  620. dispose_iface_of_platform(struct of_device* dev)
  621. {
  622. return dispose_iface(&dev->dev);
  623. }
  624. static struct of_match i2c_keywest_match[] =
  625. {
  626. {
  627. .name = OF_ANY_MATCH,
  628. .type = "i2c",
  629. .compatible = "keywest"
  630. },
  631. {},
  632. };
  633. static struct macio_driver i2c_keywest_macio_driver =
  634. {
  635. .name = "i2c-keywest",
  636. .match_table = i2c_keywest_match,
  637. .probe = create_iface_macio,
  638. .remove = dispose_iface_macio
  639. };
  640. static struct of_platform_driver i2c_keywest_of_platform_driver =
  641. {
  642. .name = "i2c-keywest",
  643. .match_table = i2c_keywest_match,
  644. .probe = create_iface_of_platform,
  645. .remove = dispose_iface_of_platform
  646. };
  647. static int __init
  648. i2c_keywest_init(void)
  649. {
  650. of_register_driver(&i2c_keywest_of_platform_driver);
  651. macio_register_driver(&i2c_keywest_macio_driver);
  652. return 0;
  653. }
  654. static void __exit
  655. i2c_keywest_cleanup(void)
  656. {
  657. of_unregister_driver(&i2c_keywest_of_platform_driver);
  658. macio_unregister_driver(&i2c_keywest_macio_driver);
  659. }
  660. module_init(i2c_keywest_init);
  661. module_exit(i2c_keywest_cleanup);