i2c-sis5595.c 11 KB

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