i2c-amd756-s4882.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * i2c-amd756-s4882.c - i2c-amd756 extras for the Tyan S4882 motherboard
  3. *
  4. * Copyright (C) 2004, 2008 Jean Delvare <jdelvare@suse.de>
  5. *
  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. *
  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. */
  16. /*
  17. * We select the channels by sending commands to the Philips
  18. * PCA9556 chip at I2C address 0x18. The main adapter is used for
  19. * the non-multiplexed part of the bus, and 4 virtual adapters
  20. * are defined for the multiplexed addresses: 0x50-0x53 (memory
  21. * module EEPROM) located on channels 1-4, and 0x4c (LM63)
  22. * located on multiplexed channels 0 and 5-7. We define one
  23. * virtual adapter per CPU, which corresponds to two multiplexed
  24. * channels:
  25. * CPU0: virtual adapter 1, channels 1 and 0
  26. * CPU1: virtual adapter 2, channels 2 and 5
  27. * CPU2: virtual adapter 3, channels 3 and 6
  28. * CPU3: virtual adapter 4, channels 4 and 7
  29. */
  30. #include <linux/module.h>
  31. #include <linux/kernel.h>
  32. #include <linux/slab.h>
  33. #include <linux/init.h>
  34. #include <linux/i2c.h>
  35. #include <linux/mutex.h>
  36. extern struct i2c_adapter amd756_smbus;
  37. static struct i2c_adapter *s4882_adapter;
  38. static struct i2c_algorithm *s4882_algo;
  39. /* Wrapper access functions for multiplexed SMBus */
  40. static DEFINE_MUTEX(amd756_lock);
  41. static s32 amd756_access_virt0(struct i2c_adapter * adap, u16 addr,
  42. unsigned short flags, char read_write,
  43. u8 command, int size,
  44. union i2c_smbus_data * data)
  45. {
  46. int error;
  47. /* We exclude the multiplexed addresses */
  48. if (addr == 0x4c || (addr & 0xfc) == 0x50 || (addr & 0xfc) == 0x30
  49. || addr == 0x18)
  50. return -ENXIO;
  51. mutex_lock(&amd756_lock);
  52. error = amd756_smbus.algo->smbus_xfer(adap, addr, flags, read_write,
  53. command, size, data);
  54. mutex_unlock(&amd756_lock);
  55. return error;
  56. }
  57. /* We remember the last used channels combination so as to only switch
  58. channels when it is really needed. This greatly reduces the SMBus
  59. overhead, but also assumes that nobody will be writing to the PCA9556
  60. in our back. */
  61. static u8 last_channels;
  62. static inline s32 amd756_access_channel(struct i2c_adapter * adap, u16 addr,
  63. unsigned short flags, char read_write,
  64. u8 command, int size,
  65. union i2c_smbus_data * data,
  66. u8 channels)
  67. {
  68. int error;
  69. /* We exclude the non-multiplexed addresses */
  70. if (addr != 0x4c && (addr & 0xfc) != 0x50 && (addr & 0xfc) != 0x30)
  71. return -ENXIO;
  72. mutex_lock(&amd756_lock);
  73. if (last_channels != channels) {
  74. union i2c_smbus_data mplxdata;
  75. mplxdata.byte = channels;
  76. error = amd756_smbus.algo->smbus_xfer(adap, 0x18, 0,
  77. I2C_SMBUS_WRITE, 0x01,
  78. I2C_SMBUS_BYTE_DATA,
  79. &mplxdata);
  80. if (error)
  81. goto UNLOCK;
  82. last_channels = channels;
  83. }
  84. error = amd756_smbus.algo->smbus_xfer(adap, addr, flags, read_write,
  85. command, size, data);
  86. UNLOCK:
  87. mutex_unlock(&amd756_lock);
  88. return error;
  89. }
  90. static s32 amd756_access_virt1(struct i2c_adapter * adap, u16 addr,
  91. unsigned short flags, char read_write,
  92. u8 command, int size,
  93. union i2c_smbus_data * data)
  94. {
  95. /* CPU0: channels 1 and 0 enabled */
  96. return amd756_access_channel(adap, addr, flags, read_write, command,
  97. size, data, 0x03);
  98. }
  99. static s32 amd756_access_virt2(struct i2c_adapter * adap, u16 addr,
  100. unsigned short flags, char read_write,
  101. u8 command, int size,
  102. union i2c_smbus_data * data)
  103. {
  104. /* CPU1: channels 2 and 5 enabled */
  105. return amd756_access_channel(adap, addr, flags, read_write, command,
  106. size, data, 0x24);
  107. }
  108. static s32 amd756_access_virt3(struct i2c_adapter * adap, u16 addr,
  109. unsigned short flags, char read_write,
  110. u8 command, int size,
  111. union i2c_smbus_data * data)
  112. {
  113. /* CPU2: channels 3 and 6 enabled */
  114. return amd756_access_channel(adap, addr, flags, read_write, command,
  115. size, data, 0x48);
  116. }
  117. static s32 amd756_access_virt4(struct i2c_adapter * adap, u16 addr,
  118. unsigned short flags, char read_write,
  119. u8 command, int size,
  120. union i2c_smbus_data * data)
  121. {
  122. /* CPU3: channels 4 and 7 enabled */
  123. return amd756_access_channel(adap, addr, flags, read_write, command,
  124. size, data, 0x90);
  125. }
  126. static int __init amd756_s4882_init(void)
  127. {
  128. int i, error;
  129. union i2c_smbus_data ioconfig;
  130. if (!amd756_smbus.dev.parent)
  131. return -ENODEV;
  132. /* Configure the PCA9556 multiplexer */
  133. ioconfig.byte = 0x00; /* All I/O to output mode */
  134. error = i2c_smbus_xfer(&amd756_smbus, 0x18, 0, I2C_SMBUS_WRITE, 0x03,
  135. I2C_SMBUS_BYTE_DATA, &ioconfig);
  136. if (error) {
  137. dev_err(&amd756_smbus.dev, "PCA9556 configuration failed\n");
  138. error = -EIO;
  139. goto ERROR0;
  140. }
  141. /* Unregister physical bus */
  142. i2c_del_adapter(&amd756_smbus);
  143. printk(KERN_INFO "Enabling SMBus multiplexing for Tyan S4882\n");
  144. /* Define the 5 virtual adapters and algorithms structures */
  145. if (!(s4882_adapter = kzalloc(5 * sizeof(struct i2c_adapter),
  146. GFP_KERNEL))) {
  147. error = -ENOMEM;
  148. goto ERROR1;
  149. }
  150. if (!(s4882_algo = kzalloc(5 * sizeof(struct i2c_algorithm),
  151. GFP_KERNEL))) {
  152. error = -ENOMEM;
  153. goto ERROR2;
  154. }
  155. /* Fill in the new structures */
  156. s4882_algo[0] = *(amd756_smbus.algo);
  157. s4882_algo[0].smbus_xfer = amd756_access_virt0;
  158. s4882_adapter[0] = amd756_smbus;
  159. s4882_adapter[0].algo = s4882_algo;
  160. s4882_adapter[0].dev.parent = amd756_smbus.dev.parent;
  161. for (i = 1; i < 5; i++) {
  162. s4882_algo[i] = *(amd756_smbus.algo);
  163. s4882_adapter[i] = amd756_smbus;
  164. snprintf(s4882_adapter[i].name, sizeof(s4882_adapter[i].name),
  165. "SMBus 8111 adapter (CPU%d)", i-1);
  166. s4882_adapter[i].algo = s4882_algo+i;
  167. s4882_adapter[i].dev.parent = amd756_smbus.dev.parent;
  168. }
  169. s4882_algo[1].smbus_xfer = amd756_access_virt1;
  170. s4882_algo[2].smbus_xfer = amd756_access_virt2;
  171. s4882_algo[3].smbus_xfer = amd756_access_virt3;
  172. s4882_algo[4].smbus_xfer = amd756_access_virt4;
  173. /* Register virtual adapters */
  174. for (i = 0; i < 5; i++) {
  175. error = i2c_add_adapter(s4882_adapter+i);
  176. if (error) {
  177. printk(KERN_ERR "i2c-amd756-s4882: "
  178. "Virtual adapter %d registration "
  179. "failed, module not inserted\n", i);
  180. for (i--; i >= 0; i--)
  181. i2c_del_adapter(s4882_adapter+i);
  182. goto ERROR3;
  183. }
  184. }
  185. return 0;
  186. ERROR3:
  187. kfree(s4882_algo);
  188. s4882_algo = NULL;
  189. ERROR2:
  190. kfree(s4882_adapter);
  191. s4882_adapter = NULL;
  192. ERROR1:
  193. /* Restore physical bus */
  194. i2c_add_adapter(&amd756_smbus);
  195. ERROR0:
  196. return error;
  197. }
  198. static void __exit amd756_s4882_exit(void)
  199. {
  200. if (s4882_adapter) {
  201. int i;
  202. for (i = 0; i < 5; i++)
  203. i2c_del_adapter(s4882_adapter+i);
  204. kfree(s4882_adapter);
  205. s4882_adapter = NULL;
  206. }
  207. kfree(s4882_algo);
  208. s4882_algo = NULL;
  209. /* Restore physical bus */
  210. if (i2c_add_adapter(&amd756_smbus))
  211. printk(KERN_ERR "i2c-amd756-s4882: "
  212. "Physical bus restoration failed\n");
  213. }
  214. MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
  215. MODULE_DESCRIPTION("S4882 SMBus multiplexing");
  216. MODULE_LICENSE("GPL");
  217. module_init(amd756_s4882_init);
  218. module_exit(amd756_s4882_exit);