i2c_ec.c 10 KB

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