i2c_ec.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * SMBus driver for ACPI Embedded Controller ($Revision: 1.3 $)
  3. *
  4. * Copyright (c) 2002, 2005 Ducrot Bruno
  5. * Copyright (c) 2005 Rich Townsend (tiny hacks & tweaks)
  6. *
  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 version 2.
  10. */
  11. #include <linux/version.h>
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/kernel.h>
  15. #include <linux/stddef.h>
  16. #include <linux/sched.h>
  17. #include <linux/init.h>
  18. #include <linux/i2c.h>
  19. #include <linux/acpi.h>
  20. #include <linux/delay.h>
  21. #include "i2c_ec.h"
  22. #define xudelay(t) udelay(t)
  23. #define xmsleep(t) msleep(t)
  24. #define ACPI_EC_HC_COMPONENT 0x00080000
  25. #define ACPI_EC_HC_CLASS "ec_hc_smbus"
  26. #define ACPI_EC_HC_HID "ACPI0001"
  27. #define ACPI_EC_HC_DRIVER_NAME "ACPI EC HC smbus driver"
  28. #define ACPI_EC_HC_DEVICE_NAME "EC HC smbus"
  29. #define _COMPONENT ACPI_EC_HC_COMPONENT
  30. ACPI_MODULE_NAME("acpi_smbus")
  31. static int acpi_ec_hc_add(struct acpi_device *device);
  32. static int acpi_ec_hc_remove(struct acpi_device *device, int type);
  33. static struct acpi_driver acpi_ec_hc_driver = {
  34. .name = ACPI_EC_HC_DRIVER_NAME,
  35. .class = ACPI_EC_HC_CLASS,
  36. .ids = ACPI_EC_HC_HID,
  37. .ops = {
  38. .add = acpi_ec_hc_add,
  39. .remove = acpi_ec_hc_remove,
  40. },
  41. };
  42. /* Various bit mask for EC_SC (R) */
  43. #define OBF 0x01
  44. #define IBF 0x02
  45. #define CMD 0x08
  46. #define BURST 0x10
  47. #define SCI_EVT 0x20
  48. #define SMI_EVT 0x40
  49. /* Commands for EC_SC (W) */
  50. #define RD_EC 0x80
  51. #define WR_EC 0x81
  52. #define BE_EC 0x82
  53. #define BD_EC 0x83
  54. #define QR_EC 0x84
  55. /*
  56. * ACPI 2.0 chapter 13 SMBus 2.0 EC register model
  57. */
  58. #define ACPI_EC_SMB_PRTCL 0x00 /* protocol, PEC */
  59. #define ACPI_EC_SMB_STS 0x01 /* status */
  60. #define ACPI_EC_SMB_ADDR 0x02 /* address */
  61. #define ACPI_EC_SMB_CMD 0x03 /* command */
  62. #define ACPI_EC_SMB_DATA 0x04 /* 32 data registers */
  63. #define ACPI_EC_SMB_BCNT 0x24 /* number of data bytes */
  64. #define ACPI_EC_SMB_ALRM_A 0x25 /* alarm address */
  65. #define ACPI_EC_SMB_ALRM_D 0x26 /* 2 bytes alarm data */
  66. #define ACPI_EC_SMB_STS_DONE 0x80
  67. #define ACPI_EC_SMB_STS_ALRM 0x40
  68. #define ACPI_EC_SMB_STS_RES 0x20
  69. #define ACPI_EC_SMB_STS_STATUS 0x1f
  70. #define ACPI_EC_SMB_STATUS_OK 0x00
  71. #define ACPI_EC_SMB_STATUS_FAIL 0x07
  72. #define ACPI_EC_SMB_STATUS_DNAK 0x10
  73. #define ACPI_EC_SMB_STATUS_DERR 0x11
  74. #define ACPI_EC_SMB_STATUS_CMD_DENY 0x12
  75. #define ACPI_EC_SMB_STATUS_UNKNOWN 0x13
  76. #define ACPI_EC_SMB_STATUS_ACC_DENY 0x17
  77. #define ACPI_EC_SMB_STATUS_TIMEOUT 0x18
  78. #define ACPI_EC_SMB_STATUS_NOTSUP 0x19
  79. #define ACPI_EC_SMB_STATUS_BUSY 0x1A
  80. #define ACPI_EC_SMB_STATUS_PEC 0x1F
  81. #define ACPI_EC_SMB_PRTCL_WRITE 0x00
  82. #define ACPI_EC_SMB_PRTCL_READ 0x01
  83. #define ACPI_EC_SMB_PRTCL_QUICK 0x02
  84. #define ACPI_EC_SMB_PRTCL_BYTE 0x04
  85. #define ACPI_EC_SMB_PRTCL_BYTE_DATA 0x06
  86. #define ACPI_EC_SMB_PRTCL_WORD_DATA 0x08
  87. #define ACPI_EC_SMB_PRTCL_BLOCK_DATA 0x0a
  88. #define ACPI_EC_SMB_PRTCL_PROC_CALL 0x0c
  89. #define ACPI_EC_SMB_PRTCL_BLOCK_PROC_CALL 0x0d
  90. #define ACPI_EC_SMB_PRTCL_I2C_BLOCK_DATA 0x4a
  91. #define ACPI_EC_SMB_PRTCL_PEC 0x80
  92. /* Length of pre/post transaction sleep (msec) */
  93. #define ACPI_EC_SMB_TRANSACTION_SLEEP 1
  94. #define ACPI_EC_SMB_ACCESS_SLEEP1 1
  95. #define ACPI_EC_SMB_ACCESS_SLEEP2 10
  96. static int acpi_ec_smb_read(struct acpi_ec_smbus *smbus, u8 address, u8 * data)
  97. {
  98. u8 val;
  99. int err;
  100. err = ec_read(smbus->base + address, &val);
  101. if (!err) {
  102. *data = val;
  103. }
  104. xmsleep(ACPI_EC_SMB_TRANSACTION_SLEEP);
  105. return (err);
  106. }
  107. static int acpi_ec_smb_write(struct acpi_ec_smbus *smbus, u8 address, u8 data)
  108. {
  109. int err;
  110. err = ec_write(smbus->base + address, data);
  111. return (err);
  112. }
  113. static int
  114. acpi_ec_smb_access(struct i2c_adapter *adap, u16 addr, unsigned short flags,
  115. char read_write, u8 command, int size,
  116. union i2c_smbus_data *data)
  117. {
  118. struct acpi_ec_smbus *smbus = adap->algo_data;
  119. unsigned char protocol, len = 0, pec, temp[2] = { 0, 0 };
  120. int i;
  121. if (read_write == I2C_SMBUS_READ) {
  122. protocol = ACPI_EC_SMB_PRTCL_READ;
  123. } else {
  124. protocol = ACPI_EC_SMB_PRTCL_WRITE;
  125. }
  126. pec = (flags & I2C_CLIENT_PEC) ? ACPI_EC_SMB_PRTCL_PEC : 0;
  127. switch (size) {
  128. case I2C_SMBUS_QUICK:
  129. protocol |= ACPI_EC_SMB_PRTCL_QUICK;
  130. read_write = I2C_SMBUS_WRITE;
  131. break;
  132. case I2C_SMBUS_BYTE:
  133. if (read_write == I2C_SMBUS_WRITE) {
  134. acpi_ec_smb_write(smbus, ACPI_EC_SMB_DATA, data->byte);
  135. }
  136. protocol |= ACPI_EC_SMB_PRTCL_BYTE;
  137. break;
  138. case I2C_SMBUS_BYTE_DATA:
  139. acpi_ec_smb_write(smbus, ACPI_EC_SMB_CMD, command);
  140. if (read_write == I2C_SMBUS_WRITE) {
  141. acpi_ec_smb_write(smbus, ACPI_EC_SMB_DATA, data->byte);
  142. }
  143. protocol |= ACPI_EC_SMB_PRTCL_BYTE_DATA;
  144. break;
  145. case I2C_SMBUS_WORD_DATA:
  146. acpi_ec_smb_write(smbus, ACPI_EC_SMB_CMD, command);
  147. if (read_write == I2C_SMBUS_WRITE) {
  148. acpi_ec_smb_write(smbus, ACPI_EC_SMB_DATA, data->word);
  149. acpi_ec_smb_write(smbus, ACPI_EC_SMB_DATA + 1,
  150. data->word >> 8);
  151. }
  152. protocol |= ACPI_EC_SMB_PRTCL_WORD_DATA | pec;
  153. break;
  154. case I2C_SMBUS_BLOCK_DATA:
  155. acpi_ec_smb_write(smbus, ACPI_EC_SMB_CMD, command);
  156. if (read_write == I2C_SMBUS_WRITE) {
  157. len = min_t(u8, data->block[0], 32);
  158. acpi_ec_smb_write(smbus, ACPI_EC_SMB_BCNT, len);
  159. for (i = 0; i < len; i++)
  160. acpi_ec_smb_write(smbus, ACPI_EC_SMB_DATA + i,
  161. data->block[i + 1]);
  162. }
  163. protocol |= ACPI_EC_SMB_PRTCL_BLOCK_DATA | pec;
  164. break;
  165. case I2C_SMBUS_I2C_BLOCK_DATA:
  166. len = min_t(u8, data->block[0], 32);
  167. acpi_ec_smb_write(smbus, ACPI_EC_SMB_CMD, command);
  168. acpi_ec_smb_write(smbus, ACPI_EC_SMB_BCNT, len);
  169. if (read_write == I2C_SMBUS_WRITE) {
  170. for (i = 0; i < len; i++) {
  171. acpi_ec_smb_write(smbus, ACPI_EC_SMB_DATA + i,
  172. data->block[i + 1]);
  173. }
  174. }
  175. protocol |= ACPI_EC_SMB_PRTCL_I2C_BLOCK_DATA;
  176. break;
  177. case I2C_SMBUS_PROC_CALL:
  178. acpi_ec_smb_write(smbus, ACPI_EC_SMB_CMD, command);
  179. acpi_ec_smb_write(smbus, ACPI_EC_SMB_DATA, data->word);
  180. acpi_ec_smb_write(smbus, ACPI_EC_SMB_DATA + 1, data->word >> 8);
  181. protocol = ACPI_EC_SMB_PRTCL_PROC_CALL | pec;
  182. read_write = I2C_SMBUS_READ;
  183. break;
  184. case I2C_SMBUS_BLOCK_PROC_CALL:
  185. protocol |= pec;
  186. len = min_t(u8, data->block[0], 31);
  187. acpi_ec_smb_write(smbus, ACPI_EC_SMB_CMD, command);
  188. acpi_ec_smb_write(smbus, ACPI_EC_SMB_BCNT, len);
  189. for (i = 0; i < len; i++)
  190. acpi_ec_smb_write(smbus, ACPI_EC_SMB_DATA + i,
  191. data->block[i + 1]);
  192. protocol = ACPI_EC_SMB_PRTCL_BLOCK_PROC_CALL | pec;
  193. read_write = I2C_SMBUS_READ;
  194. break;
  195. default:
  196. ACPI_DEBUG_PRINT((ACPI_DB_WARN, "EC SMBus adapter: "
  197. "Unsupported transaction %d\n", size));
  198. return (-1);
  199. }
  200. acpi_ec_smb_write(smbus, ACPI_EC_SMB_ADDR, addr << 1);
  201. acpi_ec_smb_write(smbus, ACPI_EC_SMB_PRTCL, protocol);
  202. acpi_ec_smb_read(smbus, ACPI_EC_SMB_STS, temp + 0);
  203. if (~temp[0] & ACPI_EC_SMB_STS_DONE) {
  204. xudelay(500);
  205. acpi_ec_smb_read(smbus, ACPI_EC_SMB_STS, temp + 0);
  206. }
  207. if (~temp[0] & ACPI_EC_SMB_STS_DONE) {
  208. xmsleep(ACPI_EC_SMB_ACCESS_SLEEP2);
  209. acpi_ec_smb_read(smbus, ACPI_EC_SMB_STS, temp + 0);
  210. }
  211. if ((~temp[0] & ACPI_EC_SMB_STS_DONE)
  212. || (temp[0] & ACPI_EC_SMB_STS_STATUS)) {
  213. return (-1);
  214. }
  215. if (read_write == I2C_SMBUS_WRITE) {
  216. return (0);
  217. }
  218. switch (size) {
  219. case I2C_SMBUS_BYTE:
  220. case I2C_SMBUS_BYTE_DATA:
  221. acpi_ec_smb_read(smbus, ACPI_EC_SMB_DATA, &data->byte);
  222. break;
  223. case I2C_SMBUS_WORD_DATA:
  224. case I2C_SMBUS_PROC_CALL:
  225. acpi_ec_smb_read(smbus, ACPI_EC_SMB_DATA, temp + 0);
  226. acpi_ec_smb_read(smbus, ACPI_EC_SMB_DATA + 1, temp + 1);
  227. data->word = (temp[1] << 8) | temp[0];
  228. break;
  229. case I2C_SMBUS_BLOCK_DATA:
  230. case I2C_SMBUS_BLOCK_PROC_CALL:
  231. len = 0;
  232. acpi_ec_smb_read(smbus, ACPI_EC_SMB_BCNT, &len);
  233. len = min_t(u8, len, 32);
  234. case I2C_SMBUS_I2C_BLOCK_DATA:
  235. for (i = 0; i < len; i++)
  236. acpi_ec_smb_read(smbus, ACPI_EC_SMB_DATA + i,
  237. data->block + i + 1);
  238. data->block[0] = len;
  239. break;
  240. }
  241. return (0);
  242. }
  243. static u32 acpi_ec_smb_func(struct i2c_adapter *adapter)
  244. {
  245. return (I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  246. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
  247. I2C_FUNC_SMBUS_BLOCK_DATA |
  248. I2C_FUNC_SMBUS_PROC_CALL |
  249. I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
  250. I2C_FUNC_SMBUS_I2C_BLOCK | I2C_FUNC_SMBUS_HWPEC_CALC);
  251. }
  252. static const struct i2c_algorithm acpi_ec_smbus_algorithm = {
  253. .smbus_xfer = acpi_ec_smb_access,
  254. .functionality = acpi_ec_smb_func,
  255. };
  256. static int acpi_ec_hc_add(struct acpi_device *device)
  257. {
  258. int status;
  259. unsigned long val;
  260. struct acpi_ec_hc *ec_hc;
  261. struct acpi_ec_smbus *smbus;
  262. if (!device) {
  263. return -EINVAL;
  264. }
  265. ec_hc = kmalloc(sizeof(struct acpi_ec_hc), GFP_KERNEL);
  266. if (!ec_hc) {
  267. return -ENOMEM;
  268. }
  269. memset(ec_hc, 0, sizeof(struct acpi_ec_hc));
  270. smbus = kmalloc(sizeof(struct acpi_ec_smbus), GFP_KERNEL);
  271. if (!smbus) {
  272. kfree(ec_hc);
  273. return -ENOMEM;
  274. }
  275. memset(smbus, 0, sizeof(struct acpi_ec_smbus));
  276. ec_hc->handle = device->handle;
  277. strcpy(acpi_device_name(device), ACPI_EC_HC_DEVICE_NAME);
  278. strcpy(acpi_device_class(device), ACPI_EC_HC_CLASS);
  279. acpi_driver_data(device) = ec_hc;
  280. status = acpi_evaluate_integer(ec_hc->handle, "_EC", NULL, &val);
  281. if (ACPI_FAILURE(status)) {
  282. ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Error obtaining _EC\n"));
  283. kfree(ec_hc);
  284. kfree(smbus);
  285. return -EIO;
  286. }
  287. smbus->ec = acpi_driver_data(device->parent);
  288. smbus->base = (val & 0xff00ull) >> 8;
  289. smbus->alert = val & 0xffull;
  290. smbus->adapter.owner = THIS_MODULE;
  291. smbus->adapter.algo = &acpi_ec_smbus_algorithm;
  292. smbus->adapter.algo_data = smbus;
  293. if (i2c_add_adapter(&smbus->adapter)) {
  294. ACPI_DEBUG_PRINT((ACPI_DB_WARN,
  295. "EC SMBus adapter: Failed to register adapter\n"));
  296. kfree(smbus);
  297. kfree(ec_hc);
  298. return -EIO;
  299. }
  300. ec_hc->smbus = smbus;
  301. printk(KERN_INFO PREFIX "%s [%s]\n",
  302. acpi_device_name(device), acpi_device_bid(device));
  303. return AE_OK;
  304. }
  305. static int acpi_ec_hc_remove(struct acpi_device *device, int type)
  306. {
  307. struct acpi_ec_hc *ec_hc;
  308. if (!device) {
  309. return -EINVAL;
  310. }
  311. ec_hc = acpi_driver_data(device);
  312. i2c_del_adapter(&ec_hc->smbus->adapter);
  313. kfree(ec_hc->smbus);
  314. kfree(ec_hc);
  315. return AE_OK;
  316. }
  317. static int __init acpi_ec_hc_init(void)
  318. {
  319. int result;
  320. result = acpi_bus_register_driver(&acpi_ec_hc_driver);
  321. if (result < 0) {
  322. return -ENODEV;
  323. }
  324. return 0;
  325. }
  326. static void __exit acpi_ec_hc_exit(void)
  327. {
  328. acpi_bus_unregister_driver(&acpi_ec_hc_driver);
  329. }
  330. struct acpi_ec_hc *acpi_get_ec_hc(struct acpi_device *device)
  331. {
  332. return ((struct acpi_ec_hc *)acpi_driver_data(device->parent));
  333. }
  334. EXPORT_SYMBOL(acpi_get_ec_hc);
  335. module_init(acpi_ec_hc_init);
  336. module_exit(acpi_ec_hc_exit);
  337. MODULE_LICENSE("GPL");
  338. MODULE_AUTHOR("Ducrot Bruno");
  339. MODULE_DESCRIPTION("ACPI EC SMBus driver");