i2c-sis5595.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and
  3. Philip Edelbrock <phil@netroedge.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. /* Note: we assume there can only be one SIS5595 with one SMBus interface */
  17. /*
  18. Note: all have mfr. ID 0x1039.
  19. SUPPORTED PCI ID
  20. 5595 0008
  21. Note: these chips contain a 0008 device which is incompatible with the
  22. 5595. We recognize these by the presence of the listed
  23. "blacklist" PCI ID and refuse to load.
  24. NOT SUPPORTED PCI ID BLACKLIST PCI ID
  25. 540 0008 0540
  26. 550 0008 0550
  27. 5513 0008 5511
  28. 5581 0008 5597
  29. 5582 0008 5597
  30. 5597 0008 5597
  31. 5598 0008 5597/5598
  32. 630 0008 0630
  33. 645 0008 0645
  34. 646 0008 0646
  35. 648 0008 0648
  36. 650 0008 0650
  37. 651 0008 0651
  38. 730 0008 0730
  39. 735 0008 0735
  40. 745 0008 0745
  41. 746 0008 0746
  42. */
  43. /* TO DO:
  44. * Add Block Transfers (ugly, but supported by the adapter)
  45. * Add adapter resets
  46. */
  47. #include <linux/kernel.h>
  48. #include <linux/module.h>
  49. #include <linux/delay.h>
  50. #include <linux/pci.h>
  51. #include <linux/ioport.h>
  52. #include <linux/init.h>
  53. #include <linux/i2c.h>
  54. #include <linux/acpi.h>
  55. #include <linux/io.h>
  56. static int blacklist[] = {
  57. PCI_DEVICE_ID_SI_540,
  58. PCI_DEVICE_ID_SI_550,
  59. PCI_DEVICE_ID_SI_630,
  60. PCI_DEVICE_ID_SI_645,
  61. PCI_DEVICE_ID_SI_646,
  62. PCI_DEVICE_ID_SI_648,
  63. PCI_DEVICE_ID_SI_650,
  64. PCI_DEVICE_ID_SI_651,
  65. PCI_DEVICE_ID_SI_730,
  66. PCI_DEVICE_ID_SI_735,
  67. PCI_DEVICE_ID_SI_745,
  68. PCI_DEVICE_ID_SI_746,
  69. PCI_DEVICE_ID_SI_5511, /* 5513 chip has the 0008 device but that ID
  70. shows up in other chips so we use the 5511
  71. ID for recognition */
  72. PCI_DEVICE_ID_SI_5597,
  73. PCI_DEVICE_ID_SI_5598,
  74. 0, /* terminates the list */
  75. };
  76. /* Length of ISA address segment */
  77. #define SIS5595_EXTENT 8
  78. /* SIS5595 SMBus registers */
  79. #define SMB_STS_LO 0x00
  80. #define SMB_STS_HI 0x01
  81. #define SMB_CTL_LO 0x02
  82. #define SMB_CTL_HI 0x03
  83. #define SMB_ADDR 0x04
  84. #define SMB_CMD 0x05
  85. #define SMB_PCNT 0x06
  86. #define SMB_CNT 0x07
  87. #define SMB_BYTE 0x08
  88. #define SMB_DEV 0x10
  89. #define SMB_DB0 0x11
  90. #define SMB_DB1 0x12
  91. #define SMB_HAA 0x13
  92. /* PCI Address Constants */
  93. #define SMB_INDEX 0x38
  94. #define SMB_DAT 0x39
  95. #define SIS5595_ENABLE_REG 0x40
  96. #define ACPI_BASE 0x90
  97. /* Other settings */
  98. #define MAX_TIMEOUT 500
  99. /* SIS5595 constants */
  100. #define SIS5595_QUICK 0x00
  101. #define SIS5595_BYTE 0x02
  102. #define SIS5595_BYTE_DATA 0x04
  103. #define SIS5595_WORD_DATA 0x06
  104. #define SIS5595_PROC_CALL 0x08
  105. #define SIS5595_BLOCK_DATA 0x0A
  106. /* insmod parameters */
  107. /* If force_addr is set to anything different from 0, we forcibly enable
  108. the device at the given address. */
  109. static u16 force_addr;
  110. module_param(force_addr, ushort, 0);
  111. MODULE_PARM_DESC(force_addr, "Initialize the base address of the i2c controller");
  112. static struct pci_driver sis5595_driver;
  113. static unsigned short sis5595_base;
  114. static struct pci_dev *sis5595_pdev;
  115. static u8 sis5595_read(u8 reg)
  116. {
  117. outb(reg, sis5595_base + SMB_INDEX);
  118. return inb(sis5595_base + SMB_DAT);
  119. }
  120. static void sis5595_write(u8 reg, u8 data)
  121. {
  122. outb(reg, sis5595_base + SMB_INDEX);
  123. outb(data, sis5595_base + SMB_DAT);
  124. }
  125. static int __devinit sis5595_setup(struct pci_dev *SIS5595_dev)
  126. {
  127. u16 a;
  128. u8 val;
  129. int *i;
  130. int retval;
  131. /* Look for imposters */
  132. for (i = blacklist; *i != 0; i++) {
  133. struct pci_dev *dev;
  134. dev = pci_get_device(PCI_VENDOR_ID_SI, *i, NULL);
  135. if (dev) {
  136. dev_err(&SIS5595_dev->dev, "Looked for SIS5595 but found unsupported device %.4x\n", *i);
  137. pci_dev_put(dev);
  138. return -ENODEV;
  139. }
  140. }
  141. /* Determine the address of the SMBus areas */
  142. pci_read_config_word(SIS5595_dev, ACPI_BASE, &sis5595_base);
  143. if (sis5595_base == 0 && force_addr == 0) {
  144. dev_err(&SIS5595_dev->dev, "ACPI base address uninitialized - upgrade BIOS or use force_addr=0xaddr\n");
  145. return -ENODEV;
  146. }
  147. if (force_addr)
  148. sis5595_base = force_addr & ~(SIS5595_EXTENT - 1);
  149. dev_dbg(&SIS5595_dev->dev, "ACPI Base address: %04x\n", sis5595_base);
  150. /* NB: We grab just the two SMBus registers here, but this may still
  151. * interfere with ACPI :-( */
  152. retval = acpi_check_region(sis5595_base + SMB_INDEX, 2,
  153. sis5595_driver.name);
  154. if (retval)
  155. return retval;
  156. if (!request_region(sis5595_base + SMB_INDEX, 2,
  157. sis5595_driver.name)) {
  158. dev_err(&SIS5595_dev->dev, "SMBus registers 0x%04x-0x%04x already in use!\n",
  159. sis5595_base + SMB_INDEX, sis5595_base + SMB_INDEX + 1);
  160. return -ENODEV;
  161. }
  162. if (force_addr) {
  163. dev_info(&SIS5595_dev->dev, "forcing ISA address 0x%04X\n", sis5595_base);
  164. if (pci_write_config_word(SIS5595_dev, ACPI_BASE, sis5595_base)
  165. != PCIBIOS_SUCCESSFUL)
  166. goto error;
  167. if (pci_read_config_word(SIS5595_dev, ACPI_BASE, &a)
  168. != PCIBIOS_SUCCESSFUL)
  169. goto error;
  170. if ((a & ~(SIS5595_EXTENT - 1)) != sis5595_base) {
  171. /* doesn't work for some chips! */
  172. dev_err(&SIS5595_dev->dev, "force address failed - not supported?\n");
  173. goto error;
  174. }
  175. }
  176. if (pci_read_config_byte(SIS5595_dev, SIS5595_ENABLE_REG, &val)
  177. != PCIBIOS_SUCCESSFUL)
  178. goto error;
  179. if ((val & 0x80) == 0) {
  180. dev_info(&SIS5595_dev->dev, "enabling ACPI\n");
  181. if (pci_write_config_byte(SIS5595_dev, SIS5595_ENABLE_REG, val | 0x80)
  182. != PCIBIOS_SUCCESSFUL)
  183. goto error;
  184. if (pci_read_config_byte(SIS5595_dev, SIS5595_ENABLE_REG, &val)
  185. != PCIBIOS_SUCCESSFUL)
  186. goto error;
  187. if ((val & 0x80) == 0) {
  188. /* doesn't work for some chips? */
  189. dev_err(&SIS5595_dev->dev, "ACPI enable failed - not supported?\n");
  190. goto error;
  191. }
  192. }
  193. /* Everything is happy */
  194. return 0;
  195. error:
  196. release_region(sis5595_base + SMB_INDEX, 2);
  197. return -ENODEV;
  198. }
  199. static int sis5595_transaction(struct i2c_adapter *adap)
  200. {
  201. int temp;
  202. int result = 0;
  203. int timeout = 0;
  204. /* Make sure the SMBus host is ready to start transmitting */
  205. temp = sis5595_read(SMB_STS_LO) + (sis5595_read(SMB_STS_HI) << 8);
  206. if (temp != 0x00) {
  207. dev_dbg(&adap->dev, "SMBus busy (%04x). Resetting...\n", temp);
  208. sis5595_write(SMB_STS_LO, temp & 0xff);
  209. sis5595_write(SMB_STS_HI, temp >> 8);
  210. if ((temp = sis5595_read(SMB_STS_LO) + (sis5595_read(SMB_STS_HI) << 8)) != 0x00) {
  211. dev_dbg(&adap->dev, "Failed! (%02x)\n", temp);
  212. return -EBUSY;
  213. } else {
  214. dev_dbg(&adap->dev, "Successful!\n");
  215. }
  216. }
  217. /* start the transaction by setting bit 4 */
  218. sis5595_write(SMB_CTL_LO, sis5595_read(SMB_CTL_LO) | 0x10);
  219. /* We will always wait for a fraction of a second! */
  220. do {
  221. msleep(1);
  222. temp = sis5595_read(SMB_STS_LO);
  223. } while (!(temp & 0x40) && (timeout++ < MAX_TIMEOUT));
  224. /* If the SMBus is still busy, we give up */
  225. if (timeout > MAX_TIMEOUT) {
  226. dev_dbg(&adap->dev, "SMBus Timeout!\n");
  227. result = -ETIMEDOUT;
  228. }
  229. if (temp & 0x10) {
  230. dev_dbg(&adap->dev, "Error: Failed bus transaction\n");
  231. result = -ENXIO;
  232. }
  233. if (temp & 0x20) {
  234. dev_err(&adap->dev, "Bus collision! SMBus may be locked until "
  235. "next hard reset (or not...)\n");
  236. /* Clock stops and slave is stuck in mid-transmission */
  237. result = -EIO;
  238. }
  239. temp = sis5595_read(SMB_STS_LO) + (sis5595_read(SMB_STS_HI) << 8);
  240. if (temp != 0x00) {
  241. sis5595_write(SMB_STS_LO, temp & 0xff);
  242. sis5595_write(SMB_STS_HI, temp >> 8);
  243. }
  244. temp = sis5595_read(SMB_STS_LO) + (sis5595_read(SMB_STS_HI) << 8);
  245. if (temp != 0x00)
  246. dev_dbg(&adap->dev, "Failed reset at end of transaction (%02x)\n", temp);
  247. return result;
  248. }
  249. /* Return negative errno on error. */
  250. static s32 sis5595_access(struct i2c_adapter *adap, u16 addr,
  251. unsigned short flags, char read_write,
  252. u8 command, int size, union i2c_smbus_data *data)
  253. {
  254. int status;
  255. switch (size) {
  256. case I2C_SMBUS_QUICK:
  257. sis5595_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
  258. size = SIS5595_QUICK;
  259. break;
  260. case I2C_SMBUS_BYTE:
  261. sis5595_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
  262. if (read_write == I2C_SMBUS_WRITE)
  263. sis5595_write(SMB_CMD, command);
  264. size = SIS5595_BYTE;
  265. break;
  266. case I2C_SMBUS_BYTE_DATA:
  267. sis5595_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
  268. sis5595_write(SMB_CMD, command);
  269. if (read_write == I2C_SMBUS_WRITE)
  270. sis5595_write(SMB_BYTE, data->byte);
  271. size = SIS5595_BYTE_DATA;
  272. break;
  273. case I2C_SMBUS_PROC_CALL:
  274. case I2C_SMBUS_WORD_DATA:
  275. sis5595_write(SMB_ADDR, ((addr & 0x7f) << 1) | (read_write & 0x01));
  276. sis5595_write(SMB_CMD, command);
  277. if (read_write == I2C_SMBUS_WRITE) {
  278. sis5595_write(SMB_BYTE, data->word & 0xff);
  279. sis5595_write(SMB_BYTE + 1,
  280. (data->word & 0xff00) >> 8);
  281. }
  282. size = (size == I2C_SMBUS_PROC_CALL) ? SIS5595_PROC_CALL : SIS5595_WORD_DATA;
  283. break;
  284. default:
  285. dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
  286. return -EOPNOTSUPP;
  287. }
  288. sis5595_write(SMB_CTL_LO, ((size & 0x0E)));
  289. status = sis5595_transaction(adap);
  290. if (status)
  291. return status;
  292. if ((size != SIS5595_PROC_CALL) &&
  293. ((read_write == I2C_SMBUS_WRITE) || (size == SIS5595_QUICK)))
  294. return 0;
  295. switch (size) {
  296. case SIS5595_BYTE:
  297. case SIS5595_BYTE_DATA:
  298. data->byte = sis5595_read(SMB_BYTE);
  299. break;
  300. case SIS5595_WORD_DATA:
  301. case SIS5595_PROC_CALL:
  302. data->word = sis5595_read(SMB_BYTE) + (sis5595_read(SMB_BYTE + 1) << 8);
  303. break;
  304. }
  305. return 0;
  306. }
  307. static u32 sis5595_func(struct i2c_adapter *adapter)
  308. {
  309. return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  310. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  311. I2C_FUNC_SMBUS_PROC_CALL;
  312. }
  313. static const struct i2c_algorithm smbus_algorithm = {
  314. .smbus_xfer = sis5595_access,
  315. .functionality = sis5595_func,
  316. };
  317. static struct i2c_adapter sis5595_adapter = {
  318. .owner = THIS_MODULE,
  319. .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
  320. .algo = &smbus_algorithm,
  321. };
  322. static DEFINE_PCI_DEVICE_TABLE(sis5595_ids) = {
  323. { PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_503) },
  324. { 0, }
  325. };
  326. MODULE_DEVICE_TABLE (pci, sis5595_ids);
  327. static int __devinit sis5595_probe(struct pci_dev *dev, const struct pci_device_id *id)
  328. {
  329. int err;
  330. if (sis5595_setup(dev)) {
  331. dev_err(&dev->dev, "SIS5595 not detected, module not inserted.\n");
  332. return -ENODEV;
  333. }
  334. /* set up the sysfs linkage to our parent device */
  335. sis5595_adapter.dev.parent = &dev->dev;
  336. snprintf(sis5595_adapter.name, sizeof(sis5595_adapter.name),
  337. "SMBus SIS5595 adapter at %04x", sis5595_base + SMB_INDEX);
  338. err = i2c_add_adapter(&sis5595_adapter);
  339. if (err) {
  340. release_region(sis5595_base + SMB_INDEX, 2);
  341. return err;
  342. }
  343. /* Always return failure here. This is to allow other drivers to bind
  344. * to this pci device. We don't really want to have control over the
  345. * pci device, we only wanted to read as few register values from it.
  346. */
  347. sis5595_pdev = pci_dev_get(dev);
  348. return -ENODEV;
  349. }
  350. static struct pci_driver sis5595_driver = {
  351. .name = "sis5595_smbus",
  352. .id_table = sis5595_ids,
  353. .probe = sis5595_probe,
  354. };
  355. static int __init i2c_sis5595_init(void)
  356. {
  357. return pci_register_driver(&sis5595_driver);
  358. }
  359. static void __exit i2c_sis5595_exit(void)
  360. {
  361. pci_unregister_driver(&sis5595_driver);
  362. if (sis5595_pdev) {
  363. i2c_del_adapter(&sis5595_adapter);
  364. release_region(sis5595_base + SMB_INDEX, 2);
  365. pci_dev_put(sis5595_pdev);
  366. sis5595_pdev = NULL;
  367. }
  368. }
  369. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
  370. MODULE_DESCRIPTION("SIS5595 SMBus driver");
  371. MODULE_LICENSE("GPL");
  372. module_init(i2c_sis5595_init);
  373. module_exit(i2c_sis5595_exit);