i2c-keywest.c 19 KB

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