i2c-keywest.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:16k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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.     
  6.     Copyright (c) 2000 Philip Edelbrock <phil@stimpy.netroedge.com>
  7.     This program is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation; either version 2 of the License, or
  10.     (at your option) any later version.
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.     Changes:
  19.     2001/12/13 BenH New implementation
  20.     2001/12/15 BenH Add support for "byte" and "quick"
  21.                         transfers. Add i2c_xfer routine.
  22.     My understanding of the various modes supported by keywest are:
  23.      - Dumb mode : not implemented, probably direct tweaking of lines
  24.      - Standard mode : simple i2c transaction of type
  25.          S Addr R/W A Data A Data ... T
  26.      - Standard sub mode : combined 8 bit subaddr write with data read
  27.          S Addr R/W A SubAddr A Data A Data ... T
  28.      - Combined mode : Subaddress and Data sequences appended with no stop
  29.          S Addr R/W A SubAddr S Addr R/W A Data A Data ... T
  30.     Currently, this driver uses only Standard mode for i2c xfer, and
  31.     smbus byte & quick transfers ; and uses StandardSub mode for
  32.     other smbus transfers instead of combined as we need that for the
  33.     sound driver to be happy
  34. */
  35. #include <linux/module.h>
  36. #include <linux/config.h>
  37. #include <linux/version.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 <asm/io.h>
  50. #include <asm/prom.h>
  51. #include <asm/machdep.h>
  52. #include <asm/pmac_feature.h>
  53. #include "i2c-keywest.h"
  54. #define DBG(x...) do {
  55. if (debug > 0) 
  56. printk(KERN_DEBUG "KW:" x); 
  57. } while(0)
  58. MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
  59. MODULE_DESCRIPTION("I2C driver for Apple's Keywest");
  60. MODULE_LICENSE("GPL");
  61. MODULE_PARM(probe, "i");
  62. MODULE_PARM(debug, "i");
  63. EXPORT_NO_SYMBOLS;
  64. int probe = 0;
  65. int debug = 0;
  66. static struct keywest_iface *ifaces = NULL;
  67. static void
  68. do_stop(struct keywest_iface* iface, int result)
  69. {
  70. write_reg(reg_control, read_reg(reg_control) | KW_I2C_CTL_STOP);
  71. iface->state = state_stop;
  72. iface->result = result;
  73. }
  74. /* Main state machine for standard & standard sub mode */
  75. static int
  76. handle_interrupt(struct keywest_iface *iface, u8 isr)
  77. {
  78. int ack;
  79. int rearm_timer = 1;
  80. DBG("handle_interrupt(), got: %x, status: %x, state: %dn",
  81. isr, read_reg(reg_status), iface->state);
  82. if (isr == 0 && iface->state != state_stop) {
  83. do_stop(iface, -1);
  84. return rearm_timer;
  85. }
  86. if (isr & KW_I2C_IRQ_STOP && iface->state != state_stop) {
  87. iface->result = -1;
  88. iface->state = state_stop;
  89. }
  90. switch(iface->state) {
  91. case state_addr:
  92. if (!(isr & KW_I2C_IRQ_ADDR)) {
  93. do_stop(iface, -1);
  94. break;
  95. }
  96. ack = read_reg(reg_status);
  97. DBG("ack on set address: %xn", ack);
  98. if ((ack & KW_I2C_STAT_LAST_AAK) == 0) {
  99. do_stop(iface, -1);
  100. break;
  101. }
  102. /* Handle rw "quick" mode */
  103. if (iface->datalen == 0)
  104. do_stop(iface, 0);
  105. else if (iface->read_write == I2C_SMBUS_READ) {
  106. iface->state = state_read;
  107. if (iface->datalen > 1)
  108. write_reg(reg_control, read_reg(reg_control)
  109. | KW_I2C_CTL_AAK);
  110. } else {
  111. iface->state = state_write;
  112. DBG("write byte: %xn", *(iface->data));
  113. write_reg(reg_data, *(iface->data++));
  114. iface->datalen--;
  115. }
  116. break;
  117. case state_read:
  118. if (!(isr & KW_I2C_IRQ_DATA)) {
  119. do_stop(iface, -1);
  120. break;
  121. }
  122. *(iface->data++) = read_reg(reg_data);
  123. DBG("read byte: %xn", *(iface->data-1));
  124. iface->datalen--;
  125. if (iface->datalen == 0)
  126. iface->state = state_stop;
  127. else
  128. write_reg(reg_control, 0);
  129. break;
  130. case state_write:
  131. if (!(isr & KW_I2C_IRQ_DATA)) {
  132. do_stop(iface, -1);
  133. break;
  134. }
  135. /* Check ack status */
  136. ack = read_reg(reg_status);
  137. DBG("ack on data write: %xn", ack);
  138. if ((ack & KW_I2C_STAT_LAST_AAK) == 0) {
  139. do_stop(iface, -1);
  140. break;
  141. }
  142. if (iface->datalen) {
  143. DBG("write byte: %xn", *(iface->data));
  144. write_reg(reg_data, *(iface->data++));
  145. iface->datalen--;
  146. } else
  147. do_stop(iface, 0);
  148. break;
  149. case state_stop:
  150. if (!(isr & KW_I2C_IRQ_STOP) && (++iface->stopretry) < 10)
  151. do_stop(iface, -1);
  152. else {
  153. rearm_timer = 0;
  154. iface->state = state_idle;
  155. write_reg(reg_control, 0x00);
  156. write_reg(reg_ier, 0x00);
  157. complete(&iface->complete);
  158. }
  159. break;
  160. }
  161. write_reg(reg_isr, isr);
  162. return rearm_timer;
  163. }
  164. /* Interrupt handler */
  165. static void
  166. keywest_irq(int irq, void *dev_id, struct pt_regs *regs)
  167. {
  168. struct keywest_iface *iface = (struct keywest_iface *)dev_id;
  169. spin_lock(&iface->lock);
  170. del_timer(&iface->timeout_timer);
  171. if (handle_interrupt(iface, read_reg(reg_isr))) {
  172. iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
  173. add_timer(&iface->timeout_timer);
  174. }
  175. spin_unlock(&iface->lock);
  176. }
  177. static void
  178. keywest_timeout(unsigned long data)
  179. {
  180. struct keywest_iface *iface = (struct keywest_iface *)data;
  181. DBG("timeout !n");
  182. spin_lock_irq(&iface->lock);
  183. if (handle_interrupt(iface, read_reg(reg_isr))) {
  184. iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
  185. add_timer(&iface->timeout_timer);
  186. }
  187. spin_unlock(&iface->lock);
  188. }
  189. /*
  190.  * SMBUS-type transfer entrypoint
  191.  */
  192. static s32
  193. keywest_smbus_xfer( struct i2c_adapter* adap,
  194. u16 addr,
  195. unsigned short flags,
  196. char read_write,
  197. u8 command,
  198. int size,
  199. union i2c_smbus_data* data)
  200. {
  201. struct keywest_chan* chan = (struct keywest_chan*)adap->data;
  202. struct keywest_iface* iface = chan->iface;
  203. int len;
  204. u8* buffer;
  205. u16 cur_word;
  206. int rc = 0;
  207. if (iface->state == state_dead)
  208. return -1;
  209. /* Prepare datas & select mode */
  210. iface->cur_mode &= ~KW_I2C_MODE_MODE_MASK;
  211. switch (size) {
  212.     case I2C_SMBUS_QUICK:
  213.      len = 0;
  214.      buffer = NULL;
  215.      iface->cur_mode |= KW_I2C_MODE_STANDARD;
  216.      break;
  217.     case I2C_SMBUS_BYTE:
  218.      len = 1;
  219.      buffer = &data->byte;
  220.      iface->cur_mode |= KW_I2C_MODE_STANDARD;
  221.      break;
  222.     case I2C_SMBUS_BYTE_DATA:
  223.      len = 1;
  224.      buffer = &data->byte;
  225.      iface->cur_mode |= KW_I2C_MODE_STANDARDSUB;
  226.      break;
  227.     case I2C_SMBUS_WORD_DATA:
  228.      len = 2;
  229.      cur_word = cpu_to_le16(data->word);
  230.      buffer = (u8 *)&cur_word;
  231.      iface->cur_mode |= KW_I2C_MODE_STANDARDSUB;
  232. break;
  233.     case I2C_SMBUS_BLOCK_DATA:
  234.      len = data->block[0];
  235.      buffer = &data->block[1];
  236.      iface->cur_mode |= KW_I2C_MODE_STANDARDSUB;
  237. break;
  238.     default:
  239.      return -1;
  240. }
  241. /* Original driver had this limitation */
  242. if (len > 32)
  243. len = 32;
  244. down(&iface->sem);
  245. DBG("chan: %d, addr: 0x%x, transfer len: %d, read: %dn",
  246. chan->chan_no, addr, len, read_write == I2C_SMBUS_READ);
  247. iface->data = buffer;
  248. iface->datalen = len;
  249. iface->state = state_addr;
  250. iface->result = 0;
  251. iface->stopretry = 0;
  252. iface->read_write = read_write;
  253. /* Setup channel & clear pending irqs */
  254. write_reg(reg_mode, iface->cur_mode | (chan->chan_no << 4));
  255. write_reg(reg_isr, read_reg(reg_isr));
  256. write_reg(reg_status, 0);
  257. /* Set up address and r/w bit */
  258. write_reg(reg_addr,
  259. (addr << 1) | ((read_write == I2C_SMBUS_READ) ? 0x01 : 0x00));
  260. /* Set up the sub address */
  261. if ((iface->cur_mode & KW_I2C_MODE_MODE_MASK) == KW_I2C_MODE_STANDARDSUB
  262.     || (iface->cur_mode & KW_I2C_MODE_MODE_MASK) == KW_I2C_MODE_COMBINED)
  263. write_reg(reg_subaddr, command);
  264. /* Arm timeout */
  265. iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
  266. add_timer(&iface->timeout_timer);
  267. /* Start sending address & enable interrupt*/
  268. write_reg(reg_control, read_reg(reg_control) | KW_I2C_CTL_XADDR);
  269. write_reg(reg_ier, KW_I2C_IRQ_MASK);
  270. wait_for_completion(&iface->complete);
  271. rc = iface->result;
  272. DBG("transfer done, result: %dn", rc);
  273. if (rc == 0 && size == I2C_SMBUS_WORD_DATA && read_write == I2C_SMBUS_READ)
  274.      data->word = le16_to_cpu(cur_word);
  275. /* Release sem */
  276. up(&iface->sem);
  277. return rc;
  278. }
  279. /*
  280.  * Generic i2c master transfer entrypoint
  281.  */
  282. static int
  283. keywest_xfer( struct i2c_adapter *adap,
  284. struct i2c_msg msgs[], 
  285. int num)
  286. {
  287. struct keywest_chan* chan = (struct keywest_chan*)adap->data;
  288. struct keywest_iface* iface = chan->iface;
  289. struct i2c_msg *pmsg;
  290. int i, completed;
  291. int rc = 0;
  292. down(&iface->sem);
  293. /* Set adapter to standard mode */
  294. iface->cur_mode &= ~KW_I2C_MODE_MODE_MASK;
  295. iface->cur_mode |= KW_I2C_MODE_STANDARD;
  296. completed = 0;
  297. for (i = 0; rc >= 0 && i < num;) {
  298. u8 addr;
  299. pmsg = &msgs[i++];
  300. addr = pmsg->addr;
  301. if (pmsg->flags & I2C_M_TEN) {
  302. printk(KERN_ERR "i2c-keywest: 10 bits addr not supported !n");
  303. rc = -EINVAL;
  304. break;
  305. }
  306. DBG("xfer: chan: %d, doing %s %d bytes to 0x%02x - %d of %d messagesn",
  307.      chan->chan_no,
  308.      pmsg->flags & I2C_M_RD ? "read" : "write",
  309.                      pmsg->len, addr, i, num);
  310.     
  311. /* Setup channel & clear pending irqs */
  312. write_reg(reg_mode, iface->cur_mode | (chan->chan_no << 4));
  313. write_reg(reg_isr, read_reg(reg_isr));
  314. write_reg(reg_status, 0);
  315. iface->data = pmsg->buf;
  316. iface->datalen = pmsg->len;
  317. iface->state = state_addr;
  318. iface->result = 0;
  319. iface->stopretry = 0;
  320. if (pmsg->flags & I2C_M_RD)
  321. iface->read_write = I2C_SMBUS_READ;
  322. else
  323. iface->read_write = I2C_SMBUS_WRITE;
  324. /* Set up address and r/w bit */
  325. if (pmsg->flags & I2C_M_REV_DIR_ADDR)
  326. addr ^= 1;
  327. write_reg(reg_addr,
  328. (addr << 1) |
  329. ((iface->read_write == I2C_SMBUS_READ) ? 0x01 : 0x00));
  330. /* Arm timeout */
  331. iface->timeout_timer.expires = jiffies + POLL_TIMEOUT;
  332. add_timer(&iface->timeout_timer);
  333. /* Start sending address & enable interrupt*/
  334. write_reg(reg_control, read_reg(reg_control) | KW_I2C_CTL_XADDR);
  335. write_reg(reg_ier, KW_I2C_IRQ_MASK);
  336. wait_for_completion(&iface->complete);
  337. rc = iface->result;
  338. if (rc == 0)
  339. completed++;
  340. DBG("transfer done, result: %dn", rc);
  341. }
  342. /* Release sem */
  343. up(&iface->sem);
  344. return completed;
  345. }
  346. static u32
  347. keywest_func(struct i2c_adapter * adapter)
  348. {
  349. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  350.        I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  351.        I2C_FUNC_SMBUS_BLOCK_DATA;
  352. }
  353. static void
  354. keywest_inc(struct i2c_adapter *adapter)
  355. {
  356. MOD_INC_USE_COUNT;
  357. }
  358. static void
  359. keywest_dec(struct i2c_adapter *adapter)
  360. {
  361. MOD_DEC_USE_COUNT;
  362. }
  363. /* For now, we only handle combined mode (smbus) */
  364. static struct i2c_algorithm keywest_algorithm = {
  365. name: "Keywest i2c",
  366. id: I2C_ALGO_SMBUS,
  367. smbus_xfer: keywest_smbus_xfer,
  368. master_xfer: keywest_xfer,
  369. functionality: keywest_func,
  370. };
  371. static int
  372. create_iface(struct device_node* np)
  373. {
  374. unsigned long steps, *psteps, *prate;
  375. unsigned bsteps, tsize, i, nchan, addroffset;
  376. struct keywest_iface* iface;
  377. int rc;
  378. psteps = (unsigned long *)get_property(np, "AAPL,address-step", NULL);
  379. steps = psteps ? (*psteps) : 0x10;
  380. /* Hrm... maybe we can be smarter here */
  381. for (bsteps = 0; (steps & 0x01) == 0; bsteps++)
  382. steps >>= 1;
  383. if (!strcmp(np->parent->name, "uni-n")) {
  384. nchan = 2;
  385. addroffset = 3;
  386. } else {
  387. addroffset = 0;
  388. nchan = 1;
  389. }
  390. tsize = sizeof(struct keywest_iface) +
  391. (sizeof(struct keywest_chan) + 4) * nchan;
  392. iface = (struct keywest_iface *) kmalloc(tsize, GFP_KERNEL);
  393. if (iface == NULL) {
  394. printk(KERN_ERR "i2c-keywest: can't allocate inteface !n");
  395. return -ENOMEM;
  396. }
  397. memset(iface, 0, tsize);
  398. init_MUTEX(&iface->sem);
  399. spin_lock_init(&iface->lock);
  400. init_completion(&iface->complete);
  401. iface->bsteps = bsteps;
  402. iface->chan_count = nchan;
  403. iface->state = state_idle;
  404. iface->irq = np->intrs[0].line;
  405. iface->channels = (struct keywest_chan *)
  406. (((unsigned long)(iface + 1) + 3UL) & ~3UL);
  407. iface->base = (unsigned long)ioremap(np->addrs[0].address + addroffset,
  408. np->addrs[0].size);
  409. if (iface->base == 0) {
  410. printk(KERN_ERR "i2c-keywest: can't map inteface !n");
  411. kfree(iface);
  412. return -ENOMEM;
  413. }
  414. init_timer(&iface->timeout_timer);
  415. iface->timeout_timer.function = keywest_timeout;
  416. iface->timeout_timer.data = (unsigned long)iface;
  417. /* Select interface rate */
  418. iface->cur_mode = KW_I2C_MODE_100KHZ;
  419. prate = (unsigned long *)get_property(np, "AAPL,i2c-rate", NULL);
  420. if (prate) switch(*prate) {
  421. case 100:
  422. iface->cur_mode = KW_I2C_MODE_100KHZ;
  423. break;
  424. case 50:
  425. iface->cur_mode = KW_I2C_MODE_50KHZ;
  426. break;
  427. case 25:
  428. iface->cur_mode = KW_I2C_MODE_25KHZ;
  429. break;
  430. default:
  431. printk(KERN_WARNING "i2c-keywest: unknown rate %ldKhz, using 100KHzn",
  432. *prate);
  433. }
  434. /* Select standard mode by default */
  435. iface->cur_mode |= KW_I2C_MODE_STANDARD;
  436. /* Write mode */
  437. write_reg(reg_mode, iface->cur_mode);
  438. /* Switch interrupts off & clear them*/
  439. write_reg(reg_ier, 0x00);
  440. write_reg(reg_isr, KW_I2C_IRQ_MASK);
  441. /* Request chip interrupt */
  442. rc = request_irq(iface->irq, keywest_irq, 0, "keywest i2c", iface);
  443. if (rc) {
  444. printk(KERN_ERR "i2c-keywest: can't get IRQ %d !n", iface->irq);
  445. iounmap((void *)iface->base);
  446. kfree(iface);
  447. return -ENODEV;
  448. }
  449. for (i=0; i<nchan; i++) {
  450. struct keywest_chan* chan = &iface->channels[i];
  451. u8 addr;
  452. sprintf(chan->adapter.name, "%s %d", np->parent->name, i);
  453. chan->iface = iface;
  454. chan->chan_no = i;
  455. chan->adapter.id = I2C_ALGO_SMBUS;
  456. chan->adapter.algo = &keywest_algorithm;
  457. chan->adapter.algo_data = NULL;
  458. chan->adapter.inc_use = keywest_inc;
  459. chan->adapter.dec_use = keywest_dec;
  460. chan->adapter.client_register = NULL;
  461. chan->adapter.client_unregister = NULL;
  462. chan->adapter.data = chan;
  463. rc = i2c_add_adapter(&chan->adapter);
  464. if (rc) {
  465. printk("i2c-keywest.c: Adapter %s registration failedn",
  466. chan->adapter.name);
  467. chan->adapter.data = NULL;
  468. }
  469. if (probe) {
  470. printk("Probe: ");
  471. for (addr = 0x00; addr <= 0x7f; addr++) {
  472. if (i2c_smbus_xfer(&chan->adapter,addr,
  473.     0,0,0,I2C_SMBUS_QUICK,NULL) >= 0)
  474. printk("%02x ", addr);
  475. }
  476. printk("n");
  477. }
  478. }
  479. printk(KERN_INFO "Found KeyWest i2c on "%s", %d channel%s, stepping: %d bitsn",
  480. np->parent->name, nchan, nchan > 1 ? "s" : "", bsteps);
  481. iface->next = ifaces;
  482. ifaces = iface;
  483. return 0;
  484. }
  485. static void
  486. dispose_iface(struct keywest_iface *iface)
  487. {
  488. int i, rc;
  489. ifaces = iface->next;
  490. /* Make sure we stop all activity */
  491. down(&iface->sem);
  492. spin_lock_irq(&iface->lock);
  493. while (iface->state != state_idle) {
  494. spin_unlock_irq(&iface->lock);
  495. set_task_state(current,TASK_UNINTERRUPTIBLE);
  496. schedule_timeout(HZ/10);
  497. spin_lock_irq(&iface->lock);
  498. }
  499. iface->state = state_dead;
  500. spin_unlock_irq(&iface->lock);
  501. free_irq(iface->irq, iface);
  502. up(&iface->sem);
  503. /* Release all channels */
  504. for (i=0; i<iface->chan_count; i++) {
  505. struct keywest_chan* chan = &iface->channels[i];
  506. if (!chan->adapter.data)
  507. continue;
  508. rc = i2c_del_adapter(&chan->adapter);
  509. chan->adapter.data = NULL;
  510. /* We aren't that prepared to deal with this... */
  511. if (rc)
  512. printk("i2c-keywest.c: i2c_del_adapter failed, that's bad !n");
  513. }
  514. iounmap((void *)iface->base);
  515. kfree(iface);
  516. }
  517. static int __init
  518. i2c_keywest_init(void)
  519. {
  520. struct device_node *np;
  521. int rc = -ENODEV;
  522. np = find_compatible_devices("i2c", "keywest");
  523. while (np != 0) {
  524. if (np->n_addrs >= 1 && np->n_intrs >= 1)
  525. rc = create_iface(np);
  526. np = np->next;
  527. }
  528. if (ifaces)
  529. rc = 0;
  530. return rc;
  531. }
  532. static void __exit
  533. i2c_keywest_cleanup(void)
  534. {
  535. while(ifaces)
  536. dispose_iface(ifaces);
  537. }
  538. module_init(i2c_keywest_init);
  539. module_exit(i2c_keywest_cleanup);