idt_89hpesx.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2016 T-Platforms. All Rights Reserved.
  4. *
  5. * IDT PCIe-switch NTB Linux driver
  6. *
  7. * Contact Information:
  8. * Serge Semin <fancer.lancer@gmail.com>, <Sergey.Semin@t-platforms.ru>
  9. */
  10. /*
  11. * NOTE of the IDT 89HPESx SMBus-slave interface driver
  12. * This driver primarily is developed to have an access to EEPROM device of
  13. * IDT PCIe-switches. IDT provides a simple SMBus interface to perform IO-
  14. * operations from/to EEPROM, which is located at private (so called Master)
  15. * SMBus of switches. Using that interface this the driver creates a simple
  16. * binary sysfs-file in the device directory:
  17. * /sys/bus/i2c/devices/<bus>-<devaddr>/eeprom
  18. * In case if read-only flag is specified in the dts-node of device desription,
  19. * User-space applications won't be able to write to the EEPROM sysfs-node.
  20. * Additionally IDT 89HPESx SMBus interface has an ability to write/read
  21. * data of device CSRs. This driver exposes debugf-file to perform simple IO
  22. * operations using that ability for just basic debug purpose. Particularly
  23. * next file is created in the specific debugfs-directory:
  24. * /sys/kernel/debug/idt_csr/
  25. * Format of the debugfs-node is:
  26. * $ cat /sys/kernel/debug/idt_csr/<bus>-<devaddr>/<devname>;
  27. * <CSR address>:<CSR value>
  28. * So reading the content of the file gives current CSR address and it value.
  29. * If User-space application wishes to change current CSR address,
  30. * it can just write a proper value to the sysfs-file:
  31. * $ echo "<CSR address>" > /sys/kernel/debug/idt_csr/<bus>-<devaddr>/<devname>
  32. * If it wants to change the CSR value as well, the format of the write
  33. * operation is:
  34. * $ echo "<CSR address>:<CSR value>" > \
  35. * /sys/kernel/debug/idt_csr/<bus>-<devaddr>/<devname>;
  36. * CSR address and value can be any of hexadecimal, decimal or octal format.
  37. */
  38. #include <linux/kernel.h>
  39. #include <linux/init.h>
  40. #include <linux/module.h>
  41. #include <linux/types.h>
  42. #include <linux/sizes.h>
  43. #include <linux/slab.h>
  44. #include <linux/mutex.h>
  45. #include <linux/sysfs.h>
  46. #include <linux/debugfs.h>
  47. #include <linux/mod_devicetable.h>
  48. #include <linux/property.h>
  49. #include <linux/i2c.h>
  50. #include <linux/pci_ids.h>
  51. #include <linux/delay.h>
  52. #define IDT_NAME "89hpesx"
  53. #define IDT_89HPESX_DESC "IDT 89HPESx SMBus-slave interface driver"
  54. #define IDT_89HPESX_VER "1.0"
  55. MODULE_DESCRIPTION(IDT_89HPESX_DESC);
  56. MODULE_VERSION(IDT_89HPESX_VER);
  57. MODULE_LICENSE("GPL v2");
  58. MODULE_AUTHOR("T-platforms");
  59. /*
  60. * csr_dbgdir - CSR read/write operations Debugfs directory
  61. */
  62. static struct dentry *csr_dbgdir;
  63. /*
  64. * struct idt_89hpesx_dev - IDT 89HPESx device data structure
  65. * @eesize: Size of EEPROM in bytes (calculated from "idt,eecompatible")
  66. * @eero: EEPROM Read-only flag
  67. * @eeaddr: EEPROM custom address
  68. *
  69. * @inieecmd: Initial cmd value for EEPROM read/write operations
  70. * @inicsrcmd: Initial cmd value for CSR read/write operations
  71. * @iniccode: Initialial command code value for IO-operations
  72. *
  73. * @csr: CSR address to perform read operation
  74. *
  75. * @smb_write: SMBus write method
  76. * @smb_read: SMBus read method
  77. * @smb_mtx: SMBus mutex
  78. *
  79. * @client: i2c client used to perform IO operations
  80. *
  81. * @ee_file: EEPROM read/write sysfs-file
  82. */
  83. struct idt_smb_seq;
  84. struct idt_89hpesx_dev {
  85. u32 eesize;
  86. bool eero;
  87. u8 eeaddr;
  88. u8 inieecmd;
  89. u8 inicsrcmd;
  90. u8 iniccode;
  91. u16 csr;
  92. int (*smb_write)(struct idt_89hpesx_dev *, const struct idt_smb_seq *);
  93. int (*smb_read)(struct idt_89hpesx_dev *, struct idt_smb_seq *);
  94. struct mutex smb_mtx;
  95. struct i2c_client *client;
  96. struct bin_attribute *ee_file;
  97. struct dentry *csr_dir;
  98. };
  99. /*
  100. * struct idt_smb_seq - sequence of data to be read/written from/to IDT 89HPESx
  101. * @ccode: SMBus command code
  102. * @bytecnt: Byte count of operation
  103. * @data: Data to by written
  104. */
  105. struct idt_smb_seq {
  106. u8 ccode;
  107. u8 bytecnt;
  108. u8 *data;
  109. };
  110. /*
  111. * struct idt_eeprom_seq - sequence of data to be read/written from/to EEPROM
  112. * @cmd: Transaction CMD
  113. * @eeaddr: EEPROM custom address
  114. * @memaddr: Internal memory address of EEPROM
  115. * @data: Data to be written at the memory address
  116. */
  117. struct idt_eeprom_seq {
  118. u8 cmd;
  119. u8 eeaddr;
  120. __le16 memaddr;
  121. u8 data;
  122. } __packed;
  123. /*
  124. * struct idt_csr_seq - sequence of data to be read/written from/to CSR
  125. * @cmd: Transaction CMD
  126. * @csraddr: Internal IDT device CSR address
  127. * @data: Data to be read/written from/to the CSR address
  128. */
  129. struct idt_csr_seq {
  130. u8 cmd;
  131. __le16 csraddr;
  132. __le32 data;
  133. } __packed;
  134. /*
  135. * SMBus command code macros
  136. * @CCODE_END: Indicates the end of transaction
  137. * @CCODE_START: Indicates the start of transaction
  138. * @CCODE_CSR: CSR read/write transaction
  139. * @CCODE_EEPROM: EEPROM read/write transaction
  140. * @CCODE_BYTE: Supplied data has BYTE length
  141. * @CCODE_WORD: Supplied data has WORD length
  142. * @CCODE_BLOCK: Supplied data has variable length passed in bytecnt
  143. * byte right following CCODE byte
  144. */
  145. #define CCODE_END ((u8)0x01)
  146. #define CCODE_START ((u8)0x02)
  147. #define CCODE_CSR ((u8)0x00)
  148. #define CCODE_EEPROM ((u8)0x04)
  149. #define CCODE_BYTE ((u8)0x00)
  150. #define CCODE_WORD ((u8)0x20)
  151. #define CCODE_BLOCK ((u8)0x40)
  152. #define CCODE_PEC ((u8)0x80)
  153. /*
  154. * EEPROM command macros
  155. * @EEPROM_OP_WRITE: EEPROM write operation
  156. * @EEPROM_OP_READ: EEPROM read operation
  157. * @EEPROM_USA: Use specified address of EEPROM
  158. * @EEPROM_NAERR: EEPROM device is not ready to respond
  159. * @EEPROM_LAERR: EEPROM arbitration loss error
  160. * @EEPROM_MSS: EEPROM misplace start & stop bits error
  161. * @EEPROM_WR_CNT: Bytes count to perform write operation
  162. * @EEPROM_WRRD_CNT: Bytes count to write before reading
  163. * @EEPROM_RD_CNT: Bytes count to perform read operation
  164. * @EEPROM_DEF_SIZE: Fall back size of EEPROM
  165. * @EEPROM_DEF_ADDR: Defatul EEPROM address
  166. * @EEPROM_TOUT: Timeout before retry read operation if eeprom is busy
  167. */
  168. #define EEPROM_OP_WRITE ((u8)0x00)
  169. #define EEPROM_OP_READ ((u8)0x01)
  170. #define EEPROM_USA ((u8)0x02)
  171. #define EEPROM_NAERR ((u8)0x08)
  172. #define EEPROM_LAERR ((u8)0x10)
  173. #define EEPROM_MSS ((u8)0x20)
  174. #define EEPROM_WR_CNT ((u8)5)
  175. #define EEPROM_WRRD_CNT ((u8)4)
  176. #define EEPROM_RD_CNT ((u8)5)
  177. #define EEPROM_DEF_SIZE ((u16)4096)
  178. #define EEPROM_DEF_ADDR ((u8)0x50)
  179. #define EEPROM_TOUT (100)
  180. /*
  181. * CSR command macros
  182. * @CSR_DWE: Enable all four bytes of the operation
  183. * @CSR_OP_WRITE: CSR write operation
  184. * @CSR_OP_READ: CSR read operation
  185. * @CSR_RERR: Read operation error
  186. * @CSR_WERR: Write operation error
  187. * @CSR_WR_CNT: Bytes count to perform write operation
  188. * @CSR_WRRD_CNT: Bytes count to write before reading
  189. * @CSR_RD_CNT: Bytes count to perform read operation
  190. * @CSR_MAX: Maximum CSR address
  191. * @CSR_DEF: Default CSR address
  192. * @CSR_REAL_ADDR: CSR real unshifted address
  193. */
  194. #define CSR_DWE ((u8)0x0F)
  195. #define CSR_OP_WRITE ((u8)0x00)
  196. #define CSR_OP_READ ((u8)0x10)
  197. #define CSR_RERR ((u8)0x40)
  198. #define CSR_WERR ((u8)0x80)
  199. #define CSR_WR_CNT ((u8)7)
  200. #define CSR_WRRD_CNT ((u8)3)
  201. #define CSR_RD_CNT ((u8)7)
  202. #define CSR_MAX ((u32)0x3FFFF)
  203. #define CSR_DEF ((u16)0x0000)
  204. #define CSR_REAL_ADDR(val) ((unsigned int)val << 2)
  205. /*
  206. * IDT 89HPESx basic register
  207. * @IDT_VIDDID_CSR: PCIe VID and DID of IDT 89HPESx
  208. * @IDT_VID_MASK: Mask of VID
  209. */
  210. #define IDT_VIDDID_CSR ((u32)0x0000)
  211. #define IDT_VID_MASK ((u32)0xFFFF)
  212. /*
  213. * IDT 89HPESx can send NACK when new command is sent before previous one
  214. * fininshed execution. In this case driver retries operation
  215. * certain times.
  216. * @RETRY_CNT: Number of retries before giving up and fail
  217. * @idt_smb_safe: Generate a retry loop on corresponding SMBus method
  218. */
  219. #define RETRY_CNT (128)
  220. #define idt_smb_safe(ops, args...) ({ \
  221. int __retry = RETRY_CNT; \
  222. s32 __sts; \
  223. do { \
  224. __sts = i2c_smbus_ ## ops ## _data(args); \
  225. } while (__retry-- && __sts < 0); \
  226. __sts; \
  227. })
  228. /*===========================================================================
  229. * i2c bus level IO-operations
  230. *===========================================================================
  231. */
  232. /*
  233. * idt_smb_write_byte() - SMBus write method when I2C_SMBUS_BYTE_DATA operation
  234. * is only available
  235. * @pdev: Pointer to the driver data
  236. * @seq: Sequence of data to be written
  237. */
  238. static int idt_smb_write_byte(struct idt_89hpesx_dev *pdev,
  239. const struct idt_smb_seq *seq)
  240. {
  241. s32 sts;
  242. u8 ccode;
  243. int idx;
  244. /* Loop over the supplied data sending byte one-by-one */
  245. for (idx = 0; idx < seq->bytecnt; idx++) {
  246. /* Collect the command code byte */
  247. ccode = seq->ccode | CCODE_BYTE;
  248. if (idx == 0)
  249. ccode |= CCODE_START;
  250. if (idx == seq->bytecnt - 1)
  251. ccode |= CCODE_END;
  252. /* Send data to the device */
  253. sts = idt_smb_safe(write_byte, pdev->client, ccode,
  254. seq->data[idx]);
  255. if (sts != 0)
  256. return (int)sts;
  257. }
  258. return 0;
  259. }
  260. /*
  261. * idt_smb_read_byte() - SMBus read method when I2C_SMBUS_BYTE_DATA operation
  262. * is only available
  263. * @pdev: Pointer to the driver data
  264. * @seq: Buffer to read data to
  265. */
  266. static int idt_smb_read_byte(struct idt_89hpesx_dev *pdev,
  267. struct idt_smb_seq *seq)
  268. {
  269. s32 sts;
  270. u8 ccode;
  271. int idx;
  272. /* Loop over the supplied buffer receiving byte one-by-one */
  273. for (idx = 0; idx < seq->bytecnt; idx++) {
  274. /* Collect the command code byte */
  275. ccode = seq->ccode | CCODE_BYTE;
  276. if (idx == 0)
  277. ccode |= CCODE_START;
  278. if (idx == seq->bytecnt - 1)
  279. ccode |= CCODE_END;
  280. /* Read data from the device */
  281. sts = idt_smb_safe(read_byte, pdev->client, ccode);
  282. if (sts < 0)
  283. return (int)sts;
  284. seq->data[idx] = (u8)sts;
  285. }
  286. return 0;
  287. }
  288. /*
  289. * idt_smb_write_word() - SMBus write method when I2C_SMBUS_BYTE_DATA and
  290. * I2C_FUNC_SMBUS_WORD_DATA operations are available
  291. * @pdev: Pointer to the driver data
  292. * @seq: Sequence of data to be written
  293. */
  294. static int idt_smb_write_word(struct idt_89hpesx_dev *pdev,
  295. const struct idt_smb_seq *seq)
  296. {
  297. s32 sts;
  298. u8 ccode;
  299. int idx, evencnt;
  300. /* Calculate the even count of data to send */
  301. evencnt = seq->bytecnt - (seq->bytecnt % 2);
  302. /* Loop over the supplied data sending two bytes at a time */
  303. for (idx = 0; idx < evencnt; idx += 2) {
  304. /* Collect the command code byte */
  305. ccode = seq->ccode | CCODE_WORD;
  306. if (idx == 0)
  307. ccode |= CCODE_START;
  308. if (idx == evencnt - 2)
  309. ccode |= CCODE_END;
  310. /* Send word data to the device */
  311. sts = idt_smb_safe(write_word, pdev->client, ccode,
  312. *(u16 *)&seq->data[idx]);
  313. if (sts != 0)
  314. return (int)sts;
  315. }
  316. /* If there is odd number of bytes then send just one last byte */
  317. if (seq->bytecnt != evencnt) {
  318. /* Collect the command code byte */
  319. ccode = seq->ccode | CCODE_BYTE | CCODE_END;
  320. if (idx == 0)
  321. ccode |= CCODE_START;
  322. /* Send byte data to the device */
  323. sts = idt_smb_safe(write_byte, pdev->client, ccode,
  324. seq->data[idx]);
  325. if (sts != 0)
  326. return (int)sts;
  327. }
  328. return 0;
  329. }
  330. /*
  331. * idt_smb_read_word() - SMBus read method when I2C_SMBUS_BYTE_DATA and
  332. * I2C_FUNC_SMBUS_WORD_DATA operations are available
  333. * @pdev: Pointer to the driver data
  334. * @seq: Buffer to read data to
  335. */
  336. static int idt_smb_read_word(struct idt_89hpesx_dev *pdev,
  337. struct idt_smb_seq *seq)
  338. {
  339. s32 sts;
  340. u8 ccode;
  341. int idx, evencnt;
  342. /* Calculate the even count of data to send */
  343. evencnt = seq->bytecnt - (seq->bytecnt % 2);
  344. /* Loop over the supplied data reading two bytes at a time */
  345. for (idx = 0; idx < evencnt; idx += 2) {
  346. /* Collect the command code byte */
  347. ccode = seq->ccode | CCODE_WORD;
  348. if (idx == 0)
  349. ccode |= CCODE_START;
  350. if (idx == evencnt - 2)
  351. ccode |= CCODE_END;
  352. /* Read word data from the device */
  353. sts = idt_smb_safe(read_word, pdev->client, ccode);
  354. if (sts < 0)
  355. return (int)sts;
  356. *(u16 *)&seq->data[idx] = (u16)sts;
  357. }
  358. /* If there is odd number of bytes then receive just one last byte */
  359. if (seq->bytecnt != evencnt) {
  360. /* Collect the command code byte */
  361. ccode = seq->ccode | CCODE_BYTE | CCODE_END;
  362. if (idx == 0)
  363. ccode |= CCODE_START;
  364. /* Read last data byte from the device */
  365. sts = idt_smb_safe(read_byte, pdev->client, ccode);
  366. if (sts < 0)
  367. return (int)sts;
  368. seq->data[idx] = (u8)sts;
  369. }
  370. return 0;
  371. }
  372. /*
  373. * idt_smb_write_block() - SMBus write method when I2C_SMBUS_BLOCK_DATA
  374. * operation is available
  375. * @pdev: Pointer to the driver data
  376. * @seq: Sequence of data to be written
  377. */
  378. static int idt_smb_write_block(struct idt_89hpesx_dev *pdev,
  379. const struct idt_smb_seq *seq)
  380. {
  381. u8 ccode;
  382. /* Return error if too much data passed to send */
  383. if (seq->bytecnt > I2C_SMBUS_BLOCK_MAX)
  384. return -EINVAL;
  385. /* Collect the command code byte */
  386. ccode = seq->ccode | CCODE_BLOCK | CCODE_START | CCODE_END;
  387. /* Send block of data to the device */
  388. return idt_smb_safe(write_block, pdev->client, ccode, seq->bytecnt,
  389. seq->data);
  390. }
  391. /*
  392. * idt_smb_read_block() - SMBus read method when I2C_SMBUS_BLOCK_DATA
  393. * operation is available
  394. * @pdev: Pointer to the driver data
  395. * @seq: Buffer to read data to
  396. */
  397. static int idt_smb_read_block(struct idt_89hpesx_dev *pdev,
  398. struct idt_smb_seq *seq)
  399. {
  400. s32 sts;
  401. u8 ccode;
  402. /* Return error if too much data passed to send */
  403. if (seq->bytecnt > I2C_SMBUS_BLOCK_MAX)
  404. return -EINVAL;
  405. /* Collect the command code byte */
  406. ccode = seq->ccode | CCODE_BLOCK | CCODE_START | CCODE_END;
  407. /* Read block of data from the device */
  408. sts = idt_smb_safe(read_block, pdev->client, ccode, seq->data);
  409. if (sts != seq->bytecnt)
  410. return (sts < 0 ? sts : -ENODATA);
  411. return 0;
  412. }
  413. /*
  414. * idt_smb_write_i2c_block() - SMBus write method when I2C_SMBUS_I2C_BLOCK_DATA
  415. * operation is available
  416. * @pdev: Pointer to the driver data
  417. * @seq: Sequence of data to be written
  418. *
  419. * NOTE It's usual SMBus write block operation, except the actual data length is
  420. * sent as first byte of data
  421. */
  422. static int idt_smb_write_i2c_block(struct idt_89hpesx_dev *pdev,
  423. const struct idt_smb_seq *seq)
  424. {
  425. u8 ccode, buf[I2C_SMBUS_BLOCK_MAX + 1];
  426. /* Return error if too much data passed to send */
  427. if (seq->bytecnt > I2C_SMBUS_BLOCK_MAX)
  428. return -EINVAL;
  429. /* Collect the data to send. Length byte must be added prior the data */
  430. buf[0] = seq->bytecnt;
  431. memcpy(&buf[1], seq->data, seq->bytecnt);
  432. /* Collect the command code byte */
  433. ccode = seq->ccode | CCODE_BLOCK | CCODE_START | CCODE_END;
  434. /* Send length and block of data to the device */
  435. return idt_smb_safe(write_i2c_block, pdev->client, ccode,
  436. seq->bytecnt + 1, buf);
  437. }
  438. /*
  439. * idt_smb_read_i2c_block() - SMBus read method when I2C_SMBUS_I2C_BLOCK_DATA
  440. * operation is available
  441. * @pdev: Pointer to the driver data
  442. * @seq: Buffer to read data to
  443. *
  444. * NOTE It's usual SMBus read block operation, except the actual data length is
  445. * retrieved as first byte of data
  446. */
  447. static int idt_smb_read_i2c_block(struct idt_89hpesx_dev *pdev,
  448. struct idt_smb_seq *seq)
  449. {
  450. u8 ccode, buf[I2C_SMBUS_BLOCK_MAX + 1];
  451. s32 sts;
  452. /* Return error if too much data passed to send */
  453. if (seq->bytecnt > I2C_SMBUS_BLOCK_MAX)
  454. return -EINVAL;
  455. /* Collect the command code byte */
  456. ccode = seq->ccode | CCODE_BLOCK | CCODE_START | CCODE_END;
  457. /* Read length and block of data from the device */
  458. sts = idt_smb_safe(read_i2c_block, pdev->client, ccode,
  459. seq->bytecnt + 1, buf);
  460. if (sts != seq->bytecnt + 1)
  461. return (sts < 0 ? sts : -ENODATA);
  462. if (buf[0] != seq->bytecnt)
  463. return -ENODATA;
  464. /* Copy retrieved data to the output data buffer */
  465. memcpy(seq->data, &buf[1], seq->bytecnt);
  466. return 0;
  467. }
  468. /*===========================================================================
  469. * EEPROM IO-operations
  470. *===========================================================================
  471. */
  472. /*
  473. * idt_eeprom_read_byte() - read just one byte from EEPROM
  474. * @pdev: Pointer to the driver data
  475. * @memaddr: Start EEPROM memory address
  476. * @data: Data to be written to EEPROM
  477. */
  478. static int idt_eeprom_read_byte(struct idt_89hpesx_dev *pdev, u16 memaddr,
  479. u8 *data)
  480. {
  481. struct device *dev = &pdev->client->dev;
  482. struct idt_eeprom_seq eeseq;
  483. struct idt_smb_seq smbseq;
  484. int ret, retry;
  485. /* Initialize SMBus sequence fields */
  486. smbseq.ccode = pdev->iniccode | CCODE_EEPROM;
  487. smbseq.data = (u8 *)&eeseq;
  488. /*
  489. * Sometimes EEPROM may respond with NACK if it's busy with previous
  490. * operation, so we need to perform a few attempts of read cycle
  491. */
  492. retry = RETRY_CNT;
  493. do {
  494. /* Send EEPROM memory address to read data from */
  495. smbseq.bytecnt = EEPROM_WRRD_CNT;
  496. eeseq.cmd = pdev->inieecmd | EEPROM_OP_READ;
  497. eeseq.eeaddr = pdev->eeaddr;
  498. eeseq.memaddr = cpu_to_le16(memaddr);
  499. ret = pdev->smb_write(pdev, &smbseq);
  500. if (ret != 0) {
  501. dev_err(dev, "Failed to init eeprom addr 0x%02x",
  502. memaddr);
  503. break;
  504. }
  505. /* Perform read operation */
  506. smbseq.bytecnt = EEPROM_RD_CNT;
  507. ret = pdev->smb_read(pdev, &smbseq);
  508. if (ret != 0) {
  509. dev_err(dev, "Failed to read eeprom data 0x%02x",
  510. memaddr);
  511. break;
  512. }
  513. /* Restart read operation if the device is busy */
  514. if (retry && (eeseq.cmd & EEPROM_NAERR)) {
  515. dev_dbg(dev, "EEPROM busy, retry reading after %d ms",
  516. EEPROM_TOUT);
  517. msleep(EEPROM_TOUT);
  518. continue;
  519. }
  520. /* Check whether IDT successfully read data from EEPROM */
  521. if (eeseq.cmd & (EEPROM_NAERR | EEPROM_LAERR | EEPROM_MSS)) {
  522. dev_err(dev,
  523. "Communication with eeprom failed, cmd 0x%hhx",
  524. eeseq.cmd);
  525. ret = -EREMOTEIO;
  526. break;
  527. }
  528. /* Save retrieved data and exit the loop */
  529. *data = eeseq.data;
  530. break;
  531. } while (retry--);
  532. /* Return the status of operation */
  533. return ret;
  534. }
  535. /*
  536. * idt_eeprom_write() - EEPROM write operation
  537. * @pdev: Pointer to the driver data
  538. * @memaddr: Start EEPROM memory address
  539. * @len: Length of data to be written
  540. * @data: Data to be written to EEPROM
  541. */
  542. static int idt_eeprom_write(struct idt_89hpesx_dev *pdev, u16 memaddr, u16 len,
  543. const u8 *data)
  544. {
  545. struct device *dev = &pdev->client->dev;
  546. struct idt_eeprom_seq eeseq;
  547. struct idt_smb_seq smbseq;
  548. int ret;
  549. u16 idx;
  550. /* Initialize SMBus sequence fields */
  551. smbseq.ccode = pdev->iniccode | CCODE_EEPROM;
  552. smbseq.data = (u8 *)&eeseq;
  553. /* Send data byte-by-byte, checking if it is successfully written */
  554. for (idx = 0; idx < len; idx++, memaddr++) {
  555. /* Lock IDT SMBus device */
  556. mutex_lock(&pdev->smb_mtx);
  557. /* Perform write operation */
  558. smbseq.bytecnt = EEPROM_WR_CNT;
  559. eeseq.cmd = pdev->inieecmd | EEPROM_OP_WRITE;
  560. eeseq.eeaddr = pdev->eeaddr;
  561. eeseq.memaddr = cpu_to_le16(memaddr);
  562. eeseq.data = data[idx];
  563. ret = pdev->smb_write(pdev, &smbseq);
  564. if (ret != 0) {
  565. dev_err(dev,
  566. "Failed to write 0x%04hx:0x%02hhx to eeprom",
  567. memaddr, data[idx]);
  568. goto err_mutex_unlock;
  569. }
  570. /*
  571. * Check whether the data is successfully written by reading
  572. * from the same EEPROM memory address.
  573. */
  574. eeseq.data = ~data[idx];
  575. ret = idt_eeprom_read_byte(pdev, memaddr, &eeseq.data);
  576. if (ret != 0)
  577. goto err_mutex_unlock;
  578. /* Check whether the read byte is the same as written one */
  579. if (eeseq.data != data[idx]) {
  580. dev_err(dev, "Values don't match 0x%02hhx != 0x%02hhx",
  581. eeseq.data, data[idx]);
  582. ret = -EREMOTEIO;
  583. goto err_mutex_unlock;
  584. }
  585. /* Unlock IDT SMBus device */
  586. err_mutex_unlock:
  587. mutex_unlock(&pdev->smb_mtx);
  588. if (ret != 0)
  589. return ret;
  590. }
  591. return 0;
  592. }
  593. /*
  594. * idt_eeprom_read() - EEPROM read operation
  595. * @pdev: Pointer to the driver data
  596. * @memaddr: Start EEPROM memory address
  597. * @len: Length of data to read
  598. * @buf: Buffer to read data to
  599. */
  600. static int idt_eeprom_read(struct idt_89hpesx_dev *pdev, u16 memaddr, u16 len,
  601. u8 *buf)
  602. {
  603. int ret;
  604. u16 idx;
  605. /* Read data byte-by-byte, retrying if it wasn't successful */
  606. for (idx = 0; idx < len; idx++, memaddr++) {
  607. /* Lock IDT SMBus device */
  608. mutex_lock(&pdev->smb_mtx);
  609. /* Just read the byte to the buffer */
  610. ret = idt_eeprom_read_byte(pdev, memaddr, &buf[idx]);
  611. /* Unlock IDT SMBus device */
  612. mutex_unlock(&pdev->smb_mtx);
  613. /* Return error if read operation failed */
  614. if (ret != 0)
  615. return ret;
  616. }
  617. return 0;
  618. }
  619. /*===========================================================================
  620. * CSR IO-operations
  621. *===========================================================================
  622. */
  623. /*
  624. * idt_csr_write() - CSR write operation
  625. * @pdev: Pointer to the driver data
  626. * @csraddr: CSR address (with no two LS bits)
  627. * @data: Data to be written to CSR
  628. */
  629. static int idt_csr_write(struct idt_89hpesx_dev *pdev, u16 csraddr,
  630. const u32 data)
  631. {
  632. struct device *dev = &pdev->client->dev;
  633. struct idt_csr_seq csrseq;
  634. struct idt_smb_seq smbseq;
  635. int ret;
  636. /* Initialize SMBus sequence fields */
  637. smbseq.ccode = pdev->iniccode | CCODE_CSR;
  638. smbseq.data = (u8 *)&csrseq;
  639. /* Lock IDT SMBus device */
  640. mutex_lock(&pdev->smb_mtx);
  641. /* Perform write operation */
  642. smbseq.bytecnt = CSR_WR_CNT;
  643. csrseq.cmd = pdev->inicsrcmd | CSR_OP_WRITE;
  644. csrseq.csraddr = cpu_to_le16(csraddr);
  645. csrseq.data = cpu_to_le32(data);
  646. ret = pdev->smb_write(pdev, &smbseq);
  647. if (ret != 0) {
  648. dev_err(dev, "Failed to write 0x%04x: 0x%04x to csr",
  649. CSR_REAL_ADDR(csraddr), data);
  650. goto err_mutex_unlock;
  651. }
  652. /* Send CSR address to read data from */
  653. smbseq.bytecnt = CSR_WRRD_CNT;
  654. csrseq.cmd = pdev->inicsrcmd | CSR_OP_READ;
  655. ret = pdev->smb_write(pdev, &smbseq);
  656. if (ret != 0) {
  657. dev_err(dev, "Failed to init csr address 0x%04x",
  658. CSR_REAL_ADDR(csraddr));
  659. goto err_mutex_unlock;
  660. }
  661. /* Perform read operation */
  662. smbseq.bytecnt = CSR_RD_CNT;
  663. ret = pdev->smb_read(pdev, &smbseq);
  664. if (ret != 0) {
  665. dev_err(dev, "Failed to read csr 0x%04x",
  666. CSR_REAL_ADDR(csraddr));
  667. goto err_mutex_unlock;
  668. }
  669. /* Check whether IDT successfully retrieved CSR data */
  670. if (csrseq.cmd & (CSR_RERR | CSR_WERR)) {
  671. dev_err(dev, "IDT failed to perform CSR r/w");
  672. ret = -EREMOTEIO;
  673. goto err_mutex_unlock;
  674. }
  675. /* Unlock IDT SMBus device */
  676. err_mutex_unlock:
  677. mutex_unlock(&pdev->smb_mtx);
  678. return ret;
  679. }
  680. /*
  681. * idt_csr_read() - CSR read operation
  682. * @pdev: Pointer to the driver data
  683. * @csraddr: CSR address (with no two LS bits)
  684. * @data: Data to be written to CSR
  685. */
  686. static int idt_csr_read(struct idt_89hpesx_dev *pdev, u16 csraddr, u32 *data)
  687. {
  688. struct device *dev = &pdev->client->dev;
  689. struct idt_csr_seq csrseq;
  690. struct idt_smb_seq smbseq;
  691. int ret;
  692. /* Initialize SMBus sequence fields */
  693. smbseq.ccode = pdev->iniccode | CCODE_CSR;
  694. smbseq.data = (u8 *)&csrseq;
  695. /* Lock IDT SMBus device */
  696. mutex_lock(&pdev->smb_mtx);
  697. /* Send CSR register address before reading it */
  698. smbseq.bytecnt = CSR_WRRD_CNT;
  699. csrseq.cmd = pdev->inicsrcmd | CSR_OP_READ;
  700. csrseq.csraddr = cpu_to_le16(csraddr);
  701. ret = pdev->smb_write(pdev, &smbseq);
  702. if (ret != 0) {
  703. dev_err(dev, "Failed to init csr address 0x%04x",
  704. CSR_REAL_ADDR(csraddr));
  705. goto err_mutex_unlock;
  706. }
  707. /* Perform read operation */
  708. smbseq.bytecnt = CSR_RD_CNT;
  709. ret = pdev->smb_read(pdev, &smbseq);
  710. if (ret != 0) {
  711. dev_err(dev, "Failed to read csr 0x%04x",
  712. CSR_REAL_ADDR(csraddr));
  713. goto err_mutex_unlock;
  714. }
  715. /* Check whether IDT successfully retrieved CSR data */
  716. if (csrseq.cmd & (CSR_RERR | CSR_WERR)) {
  717. dev_err(dev, "IDT failed to perform CSR r/w");
  718. ret = -EREMOTEIO;
  719. goto err_mutex_unlock;
  720. }
  721. /* Save data retrieved from IDT */
  722. *data = le32_to_cpu(csrseq.data);
  723. /* Unlock IDT SMBus device */
  724. err_mutex_unlock:
  725. mutex_unlock(&pdev->smb_mtx);
  726. return ret;
  727. }
  728. /*===========================================================================
  729. * Sysfs/debugfs-nodes IO-operations
  730. *===========================================================================
  731. */
  732. /*
  733. * eeprom_write() - EEPROM sysfs-node write callback
  734. * @filep: Pointer to the file system node
  735. * @kobj: Pointer to the kernel object related to the sysfs-node
  736. * @attr: Attributes of the file
  737. * @buf: Buffer to write data to
  738. * @off: Offset at which data should be written to
  739. * @count: Number of bytes to write
  740. */
  741. static ssize_t eeprom_write(struct file *filp, struct kobject *kobj,
  742. struct bin_attribute *attr,
  743. char *buf, loff_t off, size_t count)
  744. {
  745. struct idt_89hpesx_dev *pdev;
  746. int ret;
  747. /* Retrieve driver data */
  748. pdev = dev_get_drvdata(kobj_to_dev(kobj));
  749. /* Perform EEPROM write operation */
  750. ret = idt_eeprom_write(pdev, (u16)off, (u16)count, (u8 *)buf);
  751. return (ret != 0 ? ret : count);
  752. }
  753. /*
  754. * eeprom_read() - EEPROM sysfs-node read callback
  755. * @filep: Pointer to the file system node
  756. * @kobj: Pointer to the kernel object related to the sysfs-node
  757. * @attr: Attributes of the file
  758. * @buf: Buffer to write data to
  759. * @off: Offset at which data should be written to
  760. * @count: Number of bytes to write
  761. */
  762. static ssize_t eeprom_read(struct file *filp, struct kobject *kobj,
  763. struct bin_attribute *attr,
  764. char *buf, loff_t off, size_t count)
  765. {
  766. struct idt_89hpesx_dev *pdev;
  767. int ret;
  768. /* Retrieve driver data */
  769. pdev = dev_get_drvdata(kobj_to_dev(kobj));
  770. /* Perform EEPROM read operation */
  771. ret = idt_eeprom_read(pdev, (u16)off, (u16)count, (u8 *)buf);
  772. return (ret != 0 ? ret : count);
  773. }
  774. /*
  775. * idt_dbgfs_csr_write() - CSR debugfs-node write callback
  776. * @filep: Pointer to the file system file descriptor
  777. * @buf: Buffer to read data from
  778. * @count: Size of the buffer
  779. * @offp: Offset within the file
  780. *
  781. * It accepts either "0x<reg addr>:0x<value>" for saving register address
  782. * and writing value to specified DWORD register or "0x<reg addr>" for
  783. * just saving register address in order to perform next read operation.
  784. *
  785. * WARNING No spaces are allowed. Incoming string must be strictly formated as:
  786. * "<reg addr>:<value>". Register address must be aligned within 4 bytes
  787. * (one DWORD).
  788. */
  789. static ssize_t idt_dbgfs_csr_write(struct file *filep, const char __user *ubuf,
  790. size_t count, loff_t *offp)
  791. {
  792. struct idt_89hpesx_dev *pdev = filep->private_data;
  793. char *colon_ch, *csraddr_str, *csrval_str;
  794. int ret;
  795. u32 csraddr, csrval;
  796. char *buf;
  797. if (*offp)
  798. return 0;
  799. /* Copy data from User-space */
  800. buf = memdup_user_nul(ubuf, count);
  801. if (IS_ERR(buf))
  802. return PTR_ERR(buf);
  803. /* Find position of colon in the buffer */
  804. colon_ch = strnchr(buf, count, ':');
  805. /*
  806. * If there is colon passed then new CSR value should be parsed as
  807. * well, so allocate buffer for CSR address substring.
  808. * If no colon is found, then string must have just one number with
  809. * no new CSR value
  810. */
  811. if (colon_ch != NULL) {
  812. /* Copy the register address to the substring buffer */
  813. csraddr_str = kmemdup_nul(buf, colon_ch - buf, GFP_KERNEL);
  814. if (csraddr_str == NULL) {
  815. ret = -ENOMEM;
  816. goto free_buf;
  817. }
  818. /* Register value must follow the colon */
  819. csrval_str = colon_ch + 1;
  820. } else /* if (str_colon == NULL) */ {
  821. csraddr_str = (char *)buf; /* Just to shut warning up */
  822. csrval_str = NULL;
  823. }
  824. /* Convert CSR address to u32 value */
  825. ret = kstrtou32(csraddr_str, 0, &csraddr);
  826. if (ret != 0)
  827. goto free_csraddr_str;
  828. /* Check whether passed register address is valid */
  829. if (csraddr > CSR_MAX || !IS_ALIGNED(csraddr, SZ_4)) {
  830. ret = -EINVAL;
  831. goto free_csraddr_str;
  832. }
  833. /* Shift register address to the right so to have u16 address */
  834. pdev->csr = (csraddr >> 2);
  835. /* Parse new CSR value and send it to IDT, if colon has been found */
  836. if (colon_ch != NULL) {
  837. ret = kstrtou32(csrval_str, 0, &csrval);
  838. if (ret != 0)
  839. goto free_csraddr_str;
  840. ret = idt_csr_write(pdev, pdev->csr, csrval);
  841. if (ret != 0)
  842. goto free_csraddr_str;
  843. }
  844. /* Free memory only if colon has been found */
  845. free_csraddr_str:
  846. if (colon_ch != NULL)
  847. kfree(csraddr_str);
  848. /* Free buffer allocated for data retrieved from User-space */
  849. free_buf:
  850. kfree(buf);
  851. return (ret != 0 ? ret : count);
  852. }
  853. /*
  854. * idt_dbgfs_csr_read() - CSR debugfs-node read callback
  855. * @filep: Pointer to the file system file descriptor
  856. * @buf: Buffer to write data to
  857. * @count: Size of the buffer
  858. * @offp: Offset within the file
  859. *
  860. * It just prints the pair "0x<reg addr>:0x<value>" to passed buffer.
  861. */
  862. #define CSRBUF_SIZE ((size_t)32)
  863. static ssize_t idt_dbgfs_csr_read(struct file *filep, char __user *ubuf,
  864. size_t count, loff_t *offp)
  865. {
  866. struct idt_89hpesx_dev *pdev = filep->private_data;
  867. u32 csraddr, csrval;
  868. char buf[CSRBUF_SIZE];
  869. int ret, size;
  870. /* Perform CSR read operation */
  871. ret = idt_csr_read(pdev, pdev->csr, &csrval);
  872. if (ret != 0)
  873. return ret;
  874. /* Shift register address to the left so to have real address */
  875. csraddr = ((u32)pdev->csr << 2);
  876. /* Print the "0x<reg addr>:0x<value>" to buffer */
  877. size = snprintf(buf, CSRBUF_SIZE, "0x%05x:0x%08x\n",
  878. (unsigned int)csraddr, (unsigned int)csrval);
  879. /* Copy data to User-space */
  880. return simple_read_from_buffer(ubuf, count, offp, buf, size);
  881. }
  882. /*
  883. * eeprom_attribute - EEPROM sysfs-node attributes
  884. *
  885. * NOTE Size will be changed in compliance with OF node. EEPROM attribute will
  886. * be read-only as well if the corresponding flag is specified in OF node.
  887. */
  888. static BIN_ATTR_RW(eeprom, EEPROM_DEF_SIZE);
  889. /*
  890. * csr_dbgfs_ops - CSR debugfs-node read/write operations
  891. */
  892. static const struct file_operations csr_dbgfs_ops = {
  893. .owner = THIS_MODULE,
  894. .open = simple_open,
  895. .write = idt_dbgfs_csr_write,
  896. .read = idt_dbgfs_csr_read
  897. };
  898. /*===========================================================================
  899. * Driver init/deinit methods
  900. *===========================================================================
  901. */
  902. /*
  903. * idt_set_defval() - disable EEPROM access by default
  904. * @pdev: Pointer to the driver data
  905. */
  906. static void idt_set_defval(struct idt_89hpesx_dev *pdev)
  907. {
  908. /* If OF info is missing then use next values */
  909. pdev->eesize = 0;
  910. pdev->eero = true;
  911. pdev->inieecmd = 0;
  912. pdev->eeaddr = 0;
  913. }
  914. static const struct i2c_device_id ee_ids[];
  915. /*
  916. * idt_ee_match_id() - check whether the node belongs to compatible EEPROMs
  917. */
  918. static const struct i2c_device_id *idt_ee_match_id(struct fwnode_handle *fwnode)
  919. {
  920. const struct i2c_device_id *id = ee_ids;
  921. const char *compatible, *p;
  922. char devname[I2C_NAME_SIZE];
  923. int ret;
  924. ret = fwnode_property_read_string(fwnode, "compatible", &compatible);
  925. if (ret)
  926. return NULL;
  927. p = strchr(compatible, ',');
  928. strscpy(devname, p ? p + 1 : compatible, sizeof(devname));
  929. /* Search through the device name */
  930. while (id->name[0]) {
  931. if (strcmp(devname, id->name) == 0)
  932. return id;
  933. id++;
  934. }
  935. return NULL;
  936. }
  937. /*
  938. * idt_get_fw_data() - get IDT i2c-device parameters from device tree
  939. * @pdev: Pointer to the driver data
  940. */
  941. static void idt_get_fw_data(struct idt_89hpesx_dev *pdev)
  942. {
  943. struct device *dev = &pdev->client->dev;
  944. struct fwnode_handle *fwnode;
  945. const struct i2c_device_id *ee_id = NULL;
  946. u32 eeprom_addr;
  947. int ret;
  948. device_for_each_child_node(dev, fwnode) {
  949. ee_id = idt_ee_match_id(fwnode);
  950. if (ee_id)
  951. break;
  952. dev_warn(dev, "Skip unsupported EEPROM device %pfw\n", fwnode);
  953. }
  954. /* If there is no fwnode EEPROM device, then set zero size */
  955. if (!ee_id) {
  956. dev_warn(dev, "No fwnode, EEPROM access disabled");
  957. idt_set_defval(pdev);
  958. return;
  959. }
  960. /* Retrieve EEPROM size */
  961. pdev->eesize = (u32)ee_id->driver_data;
  962. /* Get custom EEPROM address from 'reg' attribute */
  963. ret = fwnode_property_read_u32(fwnode, "reg", &eeprom_addr);
  964. if (ret || (eeprom_addr == 0)) {
  965. dev_warn(dev, "No EEPROM reg found, use default address 0x%x",
  966. EEPROM_DEF_ADDR);
  967. pdev->inieecmd = 0;
  968. pdev->eeaddr = EEPROM_DEF_ADDR << 1;
  969. } else {
  970. pdev->inieecmd = EEPROM_USA;
  971. pdev->eeaddr = eeprom_addr << 1;
  972. }
  973. /* Check EEPROM 'read-only' flag */
  974. if (fwnode_property_read_bool(fwnode, "read-only"))
  975. pdev->eero = true;
  976. else /* if (!fwnode_property_read_bool(node, "read-only")) */
  977. pdev->eero = false;
  978. fwnode_handle_put(fwnode);
  979. dev_info(dev, "EEPROM of %d bytes found by 0x%x",
  980. pdev->eesize, pdev->eeaddr);
  981. }
  982. /*
  983. * idt_create_pdev() - create and init data structure of the driver
  984. * @client: i2c client of IDT PCIe-switch device
  985. */
  986. static struct idt_89hpesx_dev *idt_create_pdev(struct i2c_client *client)
  987. {
  988. struct idt_89hpesx_dev *pdev;
  989. /* Allocate memory for driver data */
  990. pdev = devm_kmalloc(&client->dev, sizeof(struct idt_89hpesx_dev),
  991. GFP_KERNEL);
  992. if (pdev == NULL)
  993. return ERR_PTR(-ENOMEM);
  994. /* Initialize basic fields of the data */
  995. pdev->client = client;
  996. i2c_set_clientdata(client, pdev);
  997. /* Read firmware nodes information */
  998. idt_get_fw_data(pdev);
  999. /* Initialize basic CSR CMD field - use full DWORD-sized r/w ops */
  1000. pdev->inicsrcmd = CSR_DWE;
  1001. pdev->csr = CSR_DEF;
  1002. /* Enable Packet Error Checking if it's supported by adapter */
  1003. if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_PEC)) {
  1004. pdev->iniccode = CCODE_PEC;
  1005. client->flags |= I2C_CLIENT_PEC;
  1006. } else /* PEC is unsupported */ {
  1007. pdev->iniccode = 0;
  1008. }
  1009. return pdev;
  1010. }
  1011. /*
  1012. * idt_free_pdev() - free data structure of the driver
  1013. * @pdev: Pointer to the driver data
  1014. */
  1015. static void idt_free_pdev(struct idt_89hpesx_dev *pdev)
  1016. {
  1017. /* Clear driver data from device private field */
  1018. i2c_set_clientdata(pdev->client, NULL);
  1019. }
  1020. /*
  1021. * idt_set_smbus_ops() - set supported SMBus operations
  1022. * @pdev: Pointer to the driver data
  1023. * Return status of smbus check operations
  1024. */
  1025. static int idt_set_smbus_ops(struct idt_89hpesx_dev *pdev)
  1026. {
  1027. struct i2c_adapter *adapter = pdev->client->adapter;
  1028. struct device *dev = &pdev->client->dev;
  1029. /* Check i2c adapter read functionality */
  1030. if (i2c_check_functionality(adapter,
  1031. I2C_FUNC_SMBUS_READ_BLOCK_DATA)) {
  1032. pdev->smb_read = idt_smb_read_block;
  1033. dev_dbg(dev, "SMBus block-read op chosen");
  1034. } else if (i2c_check_functionality(adapter,
  1035. I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
  1036. pdev->smb_read = idt_smb_read_i2c_block;
  1037. dev_dbg(dev, "SMBus i2c-block-read op chosen");
  1038. } else if (i2c_check_functionality(adapter,
  1039. I2C_FUNC_SMBUS_READ_WORD_DATA) &&
  1040. i2c_check_functionality(adapter,
  1041. I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
  1042. pdev->smb_read = idt_smb_read_word;
  1043. dev_warn(dev, "Use slow word/byte SMBus read ops");
  1044. } else if (i2c_check_functionality(adapter,
  1045. I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
  1046. pdev->smb_read = idt_smb_read_byte;
  1047. dev_warn(dev, "Use slow byte SMBus read op");
  1048. } else /* no supported smbus read operations */ {
  1049. dev_err(dev, "No supported SMBus read op");
  1050. return -EPFNOSUPPORT;
  1051. }
  1052. /* Check i2c adapter write functionality */
  1053. if (i2c_check_functionality(adapter,
  1054. I2C_FUNC_SMBUS_WRITE_BLOCK_DATA)) {
  1055. pdev->smb_write = idt_smb_write_block;
  1056. dev_dbg(dev, "SMBus block-write op chosen");
  1057. } else if (i2c_check_functionality(adapter,
  1058. I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)) {
  1059. pdev->smb_write = idt_smb_write_i2c_block;
  1060. dev_dbg(dev, "SMBus i2c-block-write op chosen");
  1061. } else if (i2c_check_functionality(adapter,
  1062. I2C_FUNC_SMBUS_WRITE_WORD_DATA) &&
  1063. i2c_check_functionality(adapter,
  1064. I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) {
  1065. pdev->smb_write = idt_smb_write_word;
  1066. dev_warn(dev, "Use slow word/byte SMBus write op");
  1067. } else if (i2c_check_functionality(adapter,
  1068. I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) {
  1069. pdev->smb_write = idt_smb_write_byte;
  1070. dev_warn(dev, "Use slow byte SMBus write op");
  1071. } else /* no supported smbus write operations */ {
  1072. dev_err(dev, "No supported SMBus write op");
  1073. return -EPFNOSUPPORT;
  1074. }
  1075. /* Initialize IDT SMBus slave interface mutex */
  1076. mutex_init(&pdev->smb_mtx);
  1077. return 0;
  1078. }
  1079. /*
  1080. * idt_check_dev() - check whether it's really IDT 89HPESx device
  1081. * @pdev: Pointer to the driver data
  1082. * Return status of i2c adapter check operation
  1083. */
  1084. static int idt_check_dev(struct idt_89hpesx_dev *pdev)
  1085. {
  1086. struct device *dev = &pdev->client->dev;
  1087. u32 viddid;
  1088. int ret;
  1089. /* Read VID and DID directly from IDT memory space */
  1090. ret = idt_csr_read(pdev, IDT_VIDDID_CSR, &viddid);
  1091. if (ret != 0) {
  1092. dev_err(dev, "Failed to read VID/DID");
  1093. return ret;
  1094. }
  1095. /* Check whether it's IDT device */
  1096. if ((viddid & IDT_VID_MASK) != PCI_VENDOR_ID_IDT) {
  1097. dev_err(dev, "Got unsupported VID/DID: 0x%08x", viddid);
  1098. return -ENODEV;
  1099. }
  1100. dev_info(dev, "Found IDT 89HPES device VID:0x%04x, DID:0x%04x",
  1101. (viddid & IDT_VID_MASK), (viddid >> 16));
  1102. return 0;
  1103. }
  1104. /*
  1105. * idt_create_sysfs_files() - create sysfs attribute files
  1106. * @pdev: Pointer to the driver data
  1107. * Return status of operation
  1108. */
  1109. static int idt_create_sysfs_files(struct idt_89hpesx_dev *pdev)
  1110. {
  1111. struct device *dev = &pdev->client->dev;
  1112. int ret;
  1113. /* Don't do anything if EEPROM isn't accessible */
  1114. if (pdev->eesize == 0) {
  1115. dev_dbg(dev, "Skip creating sysfs-files");
  1116. return 0;
  1117. }
  1118. /*
  1119. * Allocate memory for attribute file and copy the declared EEPROM attr
  1120. * structure to change some of fields
  1121. */
  1122. pdev->ee_file = devm_kmemdup(dev, &bin_attr_eeprom,
  1123. sizeof(*pdev->ee_file), GFP_KERNEL);
  1124. if (!pdev->ee_file)
  1125. return -ENOMEM;
  1126. /* In case of read-only EEPROM get rid of write ability */
  1127. if (pdev->eero) {
  1128. pdev->ee_file->attr.mode &= ~0200;
  1129. pdev->ee_file->write = NULL;
  1130. }
  1131. /* Create EEPROM sysfs file */
  1132. pdev->ee_file->size = pdev->eesize;
  1133. ret = sysfs_create_bin_file(&dev->kobj, pdev->ee_file);
  1134. if (ret != 0) {
  1135. dev_err(dev, "Failed to create EEPROM sysfs-node");
  1136. return ret;
  1137. }
  1138. return 0;
  1139. }
  1140. /*
  1141. * idt_remove_sysfs_files() - remove sysfs attribute files
  1142. * @pdev: Pointer to the driver data
  1143. */
  1144. static void idt_remove_sysfs_files(struct idt_89hpesx_dev *pdev)
  1145. {
  1146. struct device *dev = &pdev->client->dev;
  1147. /* Don't do anything if EEPROM wasn't accessible */
  1148. if (pdev->eesize == 0)
  1149. return;
  1150. /* Remove EEPROM sysfs file */
  1151. sysfs_remove_bin_file(&dev->kobj, pdev->ee_file);
  1152. }
  1153. /*
  1154. * idt_create_dbgfs_files() - create debugfs files
  1155. * @pdev: Pointer to the driver data
  1156. */
  1157. #define CSRNAME_LEN ((size_t)32)
  1158. static void idt_create_dbgfs_files(struct idt_89hpesx_dev *pdev)
  1159. {
  1160. struct i2c_client *cli = pdev->client;
  1161. char fname[CSRNAME_LEN];
  1162. /* Create Debugfs directory for CSR file */
  1163. snprintf(fname, CSRNAME_LEN, "%d-%04hx", cli->adapter->nr, cli->addr);
  1164. pdev->csr_dir = debugfs_create_dir(fname, csr_dbgdir);
  1165. /* Create Debugfs file for CSR read/write operations */
  1166. debugfs_create_file(cli->name, 0600, pdev->csr_dir, pdev,
  1167. &csr_dbgfs_ops);
  1168. }
  1169. /*
  1170. * idt_remove_dbgfs_files() - remove debugfs files
  1171. * @pdev: Pointer to the driver data
  1172. */
  1173. static void idt_remove_dbgfs_files(struct idt_89hpesx_dev *pdev)
  1174. {
  1175. /* Remove CSR directory and it sysfs-node */
  1176. debugfs_remove_recursive(pdev->csr_dir);
  1177. }
  1178. /*
  1179. * idt_probe() - IDT 89HPESx driver probe() callback method
  1180. */
  1181. static int idt_probe(struct i2c_client *client)
  1182. {
  1183. struct idt_89hpesx_dev *pdev;
  1184. int ret;
  1185. /* Create driver data */
  1186. pdev = idt_create_pdev(client);
  1187. if (IS_ERR(pdev))
  1188. return PTR_ERR(pdev);
  1189. /* Set SMBus operations */
  1190. ret = idt_set_smbus_ops(pdev);
  1191. if (ret != 0)
  1192. goto err_free_pdev;
  1193. /* Check whether it is truly IDT 89HPESx device */
  1194. ret = idt_check_dev(pdev);
  1195. if (ret != 0)
  1196. goto err_free_pdev;
  1197. /* Create sysfs files */
  1198. ret = idt_create_sysfs_files(pdev);
  1199. if (ret != 0)
  1200. goto err_free_pdev;
  1201. /* Create debugfs files */
  1202. idt_create_dbgfs_files(pdev);
  1203. return 0;
  1204. err_free_pdev:
  1205. idt_free_pdev(pdev);
  1206. return ret;
  1207. }
  1208. /*
  1209. * idt_remove() - IDT 89HPESx driver remove() callback method
  1210. */
  1211. static void idt_remove(struct i2c_client *client)
  1212. {
  1213. struct idt_89hpesx_dev *pdev = i2c_get_clientdata(client);
  1214. /* Remove debugfs files first */
  1215. idt_remove_dbgfs_files(pdev);
  1216. /* Remove sysfs files */
  1217. idt_remove_sysfs_files(pdev);
  1218. /* Discard driver data structure */
  1219. idt_free_pdev(pdev);
  1220. }
  1221. /*
  1222. * ee_ids - array of supported EEPROMs
  1223. */
  1224. static const struct i2c_device_id ee_ids[] = {
  1225. { "24c32", 4096},
  1226. { "24c64", 8192},
  1227. { "24c128", 16384},
  1228. { "24c256", 32768},
  1229. { "24c512", 65536},
  1230. {}
  1231. };
  1232. MODULE_DEVICE_TABLE(i2c, ee_ids);
  1233. /*
  1234. * idt_ids - supported IDT 89HPESx devices
  1235. */
  1236. static const struct i2c_device_id idt_ids[] = {
  1237. { "89hpes8nt2" },
  1238. { "89hpes12nt3" },
  1239. { "89hpes24nt6ag2" },
  1240. { "89hpes32nt8ag2" },
  1241. { "89hpes32nt8bg2" },
  1242. { "89hpes12nt12g2" },
  1243. { "89hpes16nt16g2" },
  1244. { "89hpes24nt24g2" },
  1245. { "89hpes32nt24ag2" },
  1246. { "89hpes32nt24bg2" },
  1247. { "89hpes12n3" },
  1248. { "89hpes12n3a" },
  1249. { "89hpes24n3" },
  1250. { "89hpes24n3a" },
  1251. { "89hpes32h8" },
  1252. { "89hpes32h8g2" },
  1253. { "89hpes48h12" },
  1254. { "89hpes48h12g2" },
  1255. { "89hpes48h12ag2" },
  1256. { "89hpes16h16" },
  1257. { "89hpes22h16" },
  1258. { "89hpes22h16g2" },
  1259. { "89hpes34h16" },
  1260. { "89hpes34h16g2" },
  1261. { "89hpes64h16" },
  1262. { "89hpes64h16g2" },
  1263. { "89hpes64h16ag2" },
  1264. /* { "89hpes3t3" }, // No SMBus-slave iface */
  1265. { "89hpes12t3g2" },
  1266. { "89hpes24t3g2" },
  1267. /* { "89hpes4t4" }, // No SMBus-slave iface */
  1268. { "89hpes16t4" },
  1269. { "89hpes4t4g2" },
  1270. { "89hpes10t4g2" },
  1271. { "89hpes16t4g2" },
  1272. { "89hpes16t4ag2" },
  1273. { "89hpes5t5" },
  1274. { "89hpes6t5" },
  1275. { "89hpes8t5" },
  1276. { "89hpes8t5a" },
  1277. { "89hpes24t6" },
  1278. { "89hpes6t6g2" },
  1279. { "89hpes24t6g2" },
  1280. { "89hpes16t7" },
  1281. { "89hpes32t8" },
  1282. { "89hpes32t8g2" },
  1283. { "89hpes48t12" },
  1284. { "89hpes48t12g2" },
  1285. { /* END OF LIST */ }
  1286. };
  1287. MODULE_DEVICE_TABLE(i2c, idt_ids);
  1288. static const struct of_device_id idt_of_match[] = {
  1289. { .compatible = "idt,89hpes8nt2", },
  1290. { .compatible = "idt,89hpes12nt3", },
  1291. { .compatible = "idt,89hpes24nt6ag2", },
  1292. { .compatible = "idt,89hpes32nt8ag2", },
  1293. { .compatible = "idt,89hpes32nt8bg2", },
  1294. { .compatible = "idt,89hpes12nt12g2", },
  1295. { .compatible = "idt,89hpes16nt16g2", },
  1296. { .compatible = "idt,89hpes24nt24g2", },
  1297. { .compatible = "idt,89hpes32nt24ag2", },
  1298. { .compatible = "idt,89hpes32nt24bg2", },
  1299. { .compatible = "idt,89hpes12n3", },
  1300. { .compatible = "idt,89hpes12n3a", },
  1301. { .compatible = "idt,89hpes24n3", },
  1302. { .compatible = "idt,89hpes24n3a", },
  1303. { .compatible = "idt,89hpes32h8", },
  1304. { .compatible = "idt,89hpes32h8g2", },
  1305. { .compatible = "idt,89hpes48h12", },
  1306. { .compatible = "idt,89hpes48h12g2", },
  1307. { .compatible = "idt,89hpes48h12ag2", },
  1308. { .compatible = "idt,89hpes16h16", },
  1309. { .compatible = "idt,89hpes22h16", },
  1310. { .compatible = "idt,89hpes22h16g2", },
  1311. { .compatible = "idt,89hpes34h16", },
  1312. { .compatible = "idt,89hpes34h16g2", },
  1313. { .compatible = "idt,89hpes64h16", },
  1314. { .compatible = "idt,89hpes64h16g2", },
  1315. { .compatible = "idt,89hpes64h16ag2", },
  1316. { .compatible = "idt,89hpes12t3g2", },
  1317. { .compatible = "idt,89hpes24t3g2", },
  1318. { .compatible = "idt,89hpes16t4", },
  1319. { .compatible = "idt,89hpes4t4g2", },
  1320. { .compatible = "idt,89hpes10t4g2", },
  1321. { .compatible = "idt,89hpes16t4g2", },
  1322. { .compatible = "idt,89hpes16t4ag2", },
  1323. { .compatible = "idt,89hpes5t5", },
  1324. { .compatible = "idt,89hpes6t5", },
  1325. { .compatible = "idt,89hpes8t5", },
  1326. { .compatible = "idt,89hpes8t5a", },
  1327. { .compatible = "idt,89hpes24t6", },
  1328. { .compatible = "idt,89hpes6t6g2", },
  1329. { .compatible = "idt,89hpes24t6g2", },
  1330. { .compatible = "idt,89hpes16t7", },
  1331. { .compatible = "idt,89hpes32t8", },
  1332. { .compatible = "idt,89hpes32t8g2", },
  1333. { .compatible = "idt,89hpes48t12", },
  1334. { .compatible = "idt,89hpes48t12g2", },
  1335. { },
  1336. };
  1337. MODULE_DEVICE_TABLE(of, idt_of_match);
  1338. /*
  1339. * idt_driver - IDT 89HPESx driver structure
  1340. */
  1341. static struct i2c_driver idt_driver = {
  1342. .driver = {
  1343. .name = IDT_NAME,
  1344. .of_match_table = idt_of_match,
  1345. },
  1346. .probe = idt_probe,
  1347. .remove = idt_remove,
  1348. .id_table = idt_ids,
  1349. };
  1350. /*
  1351. * idt_init() - IDT 89HPESx driver init() callback method
  1352. */
  1353. static int __init idt_init(void)
  1354. {
  1355. int ret;
  1356. /* Create Debugfs directory first */
  1357. if (debugfs_initialized())
  1358. csr_dbgdir = debugfs_create_dir("idt_csr", NULL);
  1359. /* Add new i2c-device driver */
  1360. ret = i2c_add_driver(&idt_driver);
  1361. if (ret) {
  1362. debugfs_remove_recursive(csr_dbgdir);
  1363. return ret;
  1364. }
  1365. return 0;
  1366. }
  1367. module_init(idt_init);
  1368. /*
  1369. * idt_exit() - IDT 89HPESx driver exit() callback method
  1370. */
  1371. static void __exit idt_exit(void)
  1372. {
  1373. /* Discard debugfs directory and all files if any */
  1374. debugfs_remove_recursive(csr_dbgdir);
  1375. /* Unregister i2c-device driver */
  1376. i2c_del_driver(&idt_driver);
  1377. }
  1378. module_exit(idt_exit);