ad525x_dpot.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ad525x_dpot: Driver for the Analog Devices digital potentiometers
  4. * Copyright (c) 2009-2010 Analog Devices, Inc.
  5. * Author: Michael Hennerich <michael.hennerich@analog.com>
  6. *
  7. * DEVID #Wipers #Positions Resistor Options (kOhm)
  8. * AD5258 1 64 1, 10, 50, 100
  9. * AD5259 1 256 5, 10, 50, 100
  10. * AD5251 2 64 1, 10, 50, 100
  11. * AD5252 2 256 1, 10, 50, 100
  12. * AD5255 3 512 25, 250
  13. * AD5253 4 64 1, 10, 50, 100
  14. * AD5254 4 256 1, 10, 50, 100
  15. * AD5160 1 256 5, 10, 50, 100
  16. * AD5161 1 256 5, 10, 50, 100
  17. * AD5162 2 256 2.5, 10, 50, 100
  18. * AD5165 1 256 100
  19. * AD5200 1 256 10, 50
  20. * AD5201 1 33 10, 50
  21. * AD5203 4 64 10, 100
  22. * AD5204 4 256 10, 50, 100
  23. * AD5206 6 256 10, 50, 100
  24. * AD5207 2 256 10, 50, 100
  25. * AD5231 1 1024 10, 50, 100
  26. * AD5232 2 256 10, 50, 100
  27. * AD5233 4 64 10, 50, 100
  28. * AD5235 2 1024 25, 250
  29. * AD5260 1 256 20, 50, 200
  30. * AD5262 2 256 20, 50, 200
  31. * AD5263 4 256 20, 50, 200
  32. * AD5290 1 256 10, 50, 100
  33. * AD5291 1 256 20, 50, 100 (20-TP)
  34. * AD5292 1 1024 20, 50, 100 (20-TP)
  35. * AD5293 1 1024 20, 50, 100
  36. * AD7376 1 128 10, 50, 100, 1M
  37. * AD8400 1 256 1, 10, 50, 100
  38. * AD8402 2 256 1, 10, 50, 100
  39. * AD8403 4 256 1, 10, 50, 100
  40. * ADN2850 3 512 25, 250
  41. * AD5241 1 256 10, 100, 1M
  42. * AD5246 1 128 5, 10, 50, 100
  43. * AD5247 1 128 5, 10, 50, 100
  44. * AD5245 1 256 5, 10, 50, 100
  45. * AD5243 2 256 2.5, 10, 50, 100
  46. * AD5248 2 256 2.5, 10, 50, 100
  47. * AD5242 2 256 20, 50, 200
  48. * AD5280 1 256 20, 50, 200
  49. * AD5282 2 256 20, 50, 200
  50. * ADN2860 3 512 25, 250
  51. * AD5273 1 64 1, 10, 50, 100 (OTP)
  52. * AD5171 1 64 5, 10, 50, 100 (OTP)
  53. * AD5170 1 256 2.5, 10, 50, 100 (OTP)
  54. * AD5172 2 256 2.5, 10, 50, 100 (OTP)
  55. * AD5173 2 256 2.5, 10, 50, 100 (OTP)
  56. * AD5270 1 1024 20, 50, 100 (50-TP)
  57. * AD5271 1 256 20, 50, 100 (50-TP)
  58. * AD5272 1 1024 20, 50, 100 (50-TP)
  59. * AD5274 1 256 20, 50, 100 (50-TP)
  60. *
  61. * See Documentation/misc-devices/ad525x_dpot.rst for more info.
  62. *
  63. * derived from ad5258.c
  64. * Copyright (c) 2009 Cyber Switching, Inc.
  65. * Author: Chris Verges <chrisv@cyberswitching.com>
  66. *
  67. * derived from ad5252.c
  68. * Copyright (c) 2006-2011 Michael Hennerich <michael.hennerich@analog.com>
  69. */
  70. #include <linux/module.h>
  71. #include <linux/device.h>
  72. #include <linux/kernel.h>
  73. #include <linux/delay.h>
  74. #include <linux/slab.h>
  75. #include <linux/string_choices.h>
  76. #include "ad525x_dpot.h"
  77. /*
  78. * Client data (each client gets its own)
  79. */
  80. struct dpot_data {
  81. struct ad_dpot_bus_data bdata;
  82. struct mutex update_lock;
  83. unsigned int rdac_mask;
  84. unsigned int max_pos;
  85. unsigned long devid;
  86. unsigned int uid;
  87. unsigned int feat;
  88. unsigned int wipers;
  89. u16 rdac_cache[MAX_RDACS];
  90. DECLARE_BITMAP(otp_en_mask, MAX_RDACS);
  91. };
  92. static inline int dpot_read_d8(struct dpot_data *dpot)
  93. {
  94. return dpot->bdata.bops->read_d8(dpot->bdata.client);
  95. }
  96. static inline int dpot_read_r8d8(struct dpot_data *dpot, u8 reg)
  97. {
  98. return dpot->bdata.bops->read_r8d8(dpot->bdata.client, reg);
  99. }
  100. static inline int dpot_read_r8d16(struct dpot_data *dpot, u8 reg)
  101. {
  102. return dpot->bdata.bops->read_r8d16(dpot->bdata.client, reg);
  103. }
  104. static inline int dpot_write_d8(struct dpot_data *dpot, u8 val)
  105. {
  106. return dpot->bdata.bops->write_d8(dpot->bdata.client, val);
  107. }
  108. static inline int dpot_write_r8d8(struct dpot_data *dpot, u8 reg, u16 val)
  109. {
  110. return dpot->bdata.bops->write_r8d8(dpot->bdata.client, reg, val);
  111. }
  112. static inline int dpot_write_r8d16(struct dpot_data *dpot, u8 reg, u16 val)
  113. {
  114. return dpot->bdata.bops->write_r8d16(dpot->bdata.client, reg, val);
  115. }
  116. static s32 dpot_read_spi(struct dpot_data *dpot, u8 reg)
  117. {
  118. unsigned int ctrl = 0;
  119. int value;
  120. if (!(reg & (DPOT_ADDR_EEPROM | DPOT_ADDR_CMD))) {
  121. if (dpot->feat & F_RDACS_WONLY)
  122. return dpot->rdac_cache[reg & DPOT_RDAC_MASK];
  123. if (dpot->uid == DPOT_UID(AD5291_ID) ||
  124. dpot->uid == DPOT_UID(AD5292_ID) ||
  125. dpot->uid == DPOT_UID(AD5293_ID)) {
  126. value = dpot_read_r8d8(dpot,
  127. DPOT_AD5291_READ_RDAC << 2);
  128. if (value < 0)
  129. return value;
  130. if (dpot->uid == DPOT_UID(AD5291_ID))
  131. value = value >> 2;
  132. return value;
  133. } else if (dpot->uid == DPOT_UID(AD5270_ID) ||
  134. dpot->uid == DPOT_UID(AD5271_ID)) {
  135. value = dpot_read_r8d8(dpot,
  136. DPOT_AD5270_1_2_4_READ_RDAC << 2);
  137. if (value < 0)
  138. return value;
  139. if (dpot->uid == DPOT_UID(AD5271_ID))
  140. value = value >> 2;
  141. return value;
  142. }
  143. ctrl = DPOT_SPI_READ_RDAC;
  144. } else if (reg & DPOT_ADDR_EEPROM) {
  145. ctrl = DPOT_SPI_READ_EEPROM;
  146. }
  147. if (dpot->feat & F_SPI_16BIT)
  148. return dpot_read_r8d8(dpot, ctrl);
  149. else if (dpot->feat & F_SPI_24BIT)
  150. return dpot_read_r8d16(dpot, ctrl);
  151. return -EFAULT;
  152. }
  153. static s32 dpot_read_i2c(struct dpot_data *dpot, u8 reg)
  154. {
  155. int value;
  156. unsigned int ctrl = 0;
  157. switch (dpot->uid) {
  158. case DPOT_UID(AD5246_ID):
  159. case DPOT_UID(AD5247_ID):
  160. return dpot_read_d8(dpot);
  161. case DPOT_UID(AD5245_ID):
  162. case DPOT_UID(AD5241_ID):
  163. case DPOT_UID(AD5242_ID):
  164. case DPOT_UID(AD5243_ID):
  165. case DPOT_UID(AD5248_ID):
  166. case DPOT_UID(AD5280_ID):
  167. case DPOT_UID(AD5282_ID):
  168. ctrl = ((reg & DPOT_RDAC_MASK) == DPOT_RDAC0) ?
  169. 0 : DPOT_AD5282_RDAC_AB;
  170. return dpot_read_r8d8(dpot, ctrl);
  171. case DPOT_UID(AD5170_ID):
  172. case DPOT_UID(AD5171_ID):
  173. case DPOT_UID(AD5273_ID):
  174. return dpot_read_d8(dpot);
  175. case DPOT_UID(AD5172_ID):
  176. case DPOT_UID(AD5173_ID):
  177. ctrl = ((reg & DPOT_RDAC_MASK) == DPOT_RDAC0) ?
  178. 0 : DPOT_AD5172_3_A0;
  179. return dpot_read_r8d8(dpot, ctrl);
  180. case DPOT_UID(AD5272_ID):
  181. case DPOT_UID(AD5274_ID):
  182. dpot_write_r8d8(dpot,
  183. (DPOT_AD5270_1_2_4_READ_RDAC << 2), 0);
  184. value = dpot_read_r8d16(dpot, DPOT_AD5270_1_2_4_RDAC << 2);
  185. if (value < 0)
  186. return value;
  187. /*
  188. * AD5272/AD5274 returns high byte first, however
  189. * underling smbus expects low byte first.
  190. */
  191. value = swab16(value);
  192. if (dpot->uid == DPOT_UID(AD5274_ID))
  193. value = value >> 2;
  194. return value;
  195. default:
  196. if ((reg & DPOT_REG_TOL) || (dpot->max_pos > 256))
  197. return dpot_read_r8d16(dpot, (reg & 0xF8) |
  198. ((reg & 0x7) << 1));
  199. else
  200. return dpot_read_r8d8(dpot, reg);
  201. }
  202. }
  203. static s32 dpot_read(struct dpot_data *dpot, u8 reg)
  204. {
  205. if (dpot->feat & F_SPI)
  206. return dpot_read_spi(dpot, reg);
  207. else
  208. return dpot_read_i2c(dpot, reg);
  209. }
  210. static s32 dpot_write_spi(struct dpot_data *dpot, u8 reg, u16 value)
  211. {
  212. unsigned int val = 0;
  213. if (!(reg & (DPOT_ADDR_EEPROM | DPOT_ADDR_CMD | DPOT_ADDR_OTP))) {
  214. if (dpot->feat & F_RDACS_WONLY)
  215. dpot->rdac_cache[reg & DPOT_RDAC_MASK] = value;
  216. if (dpot->feat & F_AD_APPDATA) {
  217. if (dpot->feat & F_SPI_8BIT) {
  218. val = ((reg & DPOT_RDAC_MASK) <<
  219. DPOT_MAX_POS(dpot->devid)) |
  220. value;
  221. return dpot_write_d8(dpot, val);
  222. } else if (dpot->feat & F_SPI_16BIT) {
  223. val = ((reg & DPOT_RDAC_MASK) <<
  224. DPOT_MAX_POS(dpot->devid)) |
  225. value;
  226. return dpot_write_r8d8(dpot, val >> 8,
  227. val & 0xFF);
  228. } else
  229. BUG();
  230. } else {
  231. if (dpot->uid == DPOT_UID(AD5291_ID) ||
  232. dpot->uid == DPOT_UID(AD5292_ID) ||
  233. dpot->uid == DPOT_UID(AD5293_ID)) {
  234. dpot_write_r8d8(dpot, DPOT_AD5291_CTRLREG << 2,
  235. DPOT_AD5291_UNLOCK_CMD);
  236. if (dpot->uid == DPOT_UID(AD5291_ID))
  237. value = value << 2;
  238. return dpot_write_r8d8(dpot,
  239. (DPOT_AD5291_RDAC << 2) |
  240. (value >> 8), value & 0xFF);
  241. } else if (dpot->uid == DPOT_UID(AD5270_ID) ||
  242. dpot->uid == DPOT_UID(AD5271_ID)) {
  243. dpot_write_r8d8(dpot,
  244. DPOT_AD5270_1_2_4_CTRLREG << 2,
  245. DPOT_AD5270_1_2_4_UNLOCK_CMD);
  246. if (dpot->uid == DPOT_UID(AD5271_ID))
  247. value = value << 2;
  248. return dpot_write_r8d8(dpot,
  249. (DPOT_AD5270_1_2_4_RDAC << 2) |
  250. (value >> 8), value & 0xFF);
  251. }
  252. val = DPOT_SPI_RDAC | (reg & DPOT_RDAC_MASK);
  253. }
  254. } else if (reg & DPOT_ADDR_EEPROM) {
  255. val = DPOT_SPI_EEPROM | (reg & DPOT_RDAC_MASK);
  256. } else if (reg & DPOT_ADDR_CMD) {
  257. switch (reg) {
  258. case DPOT_DEC_ALL_6DB:
  259. val = DPOT_SPI_DEC_ALL_6DB;
  260. break;
  261. case DPOT_INC_ALL_6DB:
  262. val = DPOT_SPI_INC_ALL_6DB;
  263. break;
  264. case DPOT_DEC_ALL:
  265. val = DPOT_SPI_DEC_ALL;
  266. break;
  267. case DPOT_INC_ALL:
  268. val = DPOT_SPI_INC_ALL;
  269. break;
  270. }
  271. } else if (reg & DPOT_ADDR_OTP) {
  272. if (dpot->uid == DPOT_UID(AD5291_ID) ||
  273. dpot->uid == DPOT_UID(AD5292_ID)) {
  274. return dpot_write_r8d8(dpot,
  275. DPOT_AD5291_STORE_XTPM << 2, 0);
  276. } else if (dpot->uid == DPOT_UID(AD5270_ID) ||
  277. dpot->uid == DPOT_UID(AD5271_ID)) {
  278. return dpot_write_r8d8(dpot,
  279. DPOT_AD5270_1_2_4_STORE_XTPM << 2, 0);
  280. }
  281. } else
  282. BUG();
  283. if (dpot->feat & F_SPI_16BIT)
  284. return dpot_write_r8d8(dpot, val, value);
  285. else if (dpot->feat & F_SPI_24BIT)
  286. return dpot_write_r8d16(dpot, val, value);
  287. return -EFAULT;
  288. }
  289. static s32 dpot_write_i2c(struct dpot_data *dpot, u8 reg, u16 value)
  290. {
  291. /* Only write the instruction byte for certain commands */
  292. unsigned int tmp = 0, ctrl = 0;
  293. switch (dpot->uid) {
  294. case DPOT_UID(AD5246_ID):
  295. case DPOT_UID(AD5247_ID):
  296. return dpot_write_d8(dpot, value);
  297. case DPOT_UID(AD5245_ID):
  298. case DPOT_UID(AD5241_ID):
  299. case DPOT_UID(AD5242_ID):
  300. case DPOT_UID(AD5243_ID):
  301. case DPOT_UID(AD5248_ID):
  302. case DPOT_UID(AD5280_ID):
  303. case DPOT_UID(AD5282_ID):
  304. ctrl = ((reg & DPOT_RDAC_MASK) == DPOT_RDAC0) ?
  305. 0 : DPOT_AD5282_RDAC_AB;
  306. return dpot_write_r8d8(dpot, ctrl, value);
  307. case DPOT_UID(AD5171_ID):
  308. case DPOT_UID(AD5273_ID):
  309. if (reg & DPOT_ADDR_OTP) {
  310. tmp = dpot_read_d8(dpot);
  311. if (tmp >> 6) /* Ready to Program? */
  312. return -EFAULT;
  313. ctrl = DPOT_AD5273_FUSE;
  314. }
  315. return dpot_write_r8d8(dpot, ctrl, value);
  316. case DPOT_UID(AD5172_ID):
  317. case DPOT_UID(AD5173_ID):
  318. ctrl = ((reg & DPOT_RDAC_MASK) == DPOT_RDAC0) ?
  319. 0 : DPOT_AD5172_3_A0;
  320. if (reg & DPOT_ADDR_OTP) {
  321. tmp = dpot_read_r8d16(dpot, ctrl);
  322. if (tmp >> 14) /* Ready to Program? */
  323. return -EFAULT;
  324. ctrl |= DPOT_AD5170_2_3_FUSE;
  325. }
  326. return dpot_write_r8d8(dpot, ctrl, value);
  327. case DPOT_UID(AD5170_ID):
  328. if (reg & DPOT_ADDR_OTP) {
  329. tmp = dpot_read_r8d16(dpot, tmp);
  330. if (tmp >> 14) /* Ready to Program? */
  331. return -EFAULT;
  332. ctrl = DPOT_AD5170_2_3_FUSE;
  333. }
  334. return dpot_write_r8d8(dpot, ctrl, value);
  335. case DPOT_UID(AD5272_ID):
  336. case DPOT_UID(AD5274_ID):
  337. dpot_write_r8d8(dpot, DPOT_AD5270_1_2_4_CTRLREG << 2,
  338. DPOT_AD5270_1_2_4_UNLOCK_CMD);
  339. if (reg & DPOT_ADDR_OTP)
  340. return dpot_write_r8d8(dpot,
  341. DPOT_AD5270_1_2_4_STORE_XTPM << 2, 0);
  342. if (dpot->uid == DPOT_UID(AD5274_ID))
  343. value = value << 2;
  344. return dpot_write_r8d8(dpot, (DPOT_AD5270_1_2_4_RDAC << 2) |
  345. (value >> 8), value & 0xFF);
  346. default:
  347. if (reg & DPOT_ADDR_CMD)
  348. return dpot_write_d8(dpot, reg);
  349. if (dpot->max_pos > 256)
  350. return dpot_write_r8d16(dpot, (reg & 0xF8) |
  351. ((reg & 0x7) << 1), value);
  352. else
  353. /* All other registers require instruction + data bytes */
  354. return dpot_write_r8d8(dpot, reg, value);
  355. }
  356. }
  357. static s32 dpot_write(struct dpot_data *dpot, u8 reg, u16 value)
  358. {
  359. if (dpot->feat & F_SPI)
  360. return dpot_write_spi(dpot, reg, value);
  361. else
  362. return dpot_write_i2c(dpot, reg, value);
  363. }
  364. /* sysfs functions */
  365. static ssize_t sysfs_show_reg(struct device *dev,
  366. struct device_attribute *attr,
  367. char *buf, u32 reg)
  368. {
  369. struct dpot_data *data = dev_get_drvdata(dev);
  370. s32 value;
  371. if (reg & DPOT_ADDR_OTP_EN)
  372. return sprintf(buf, "%s\n", str_enabled_disabled(
  373. test_bit(DPOT_RDAC_MASK & reg, data->otp_en_mask)));
  374. mutex_lock(&data->update_lock);
  375. value = dpot_read(data, reg);
  376. mutex_unlock(&data->update_lock);
  377. if (value < 0)
  378. return -EINVAL;
  379. /*
  380. * Let someone else deal with converting this ...
  381. * the tolerance is a two-byte value where the MSB
  382. * is a sign + integer value, and the LSB is a
  383. * decimal value. See page 18 of the AD5258
  384. * datasheet (Rev. A) for more details.
  385. */
  386. if (reg & DPOT_REG_TOL)
  387. return sprintf(buf, "0x%04x\n", value & 0xFFFF);
  388. else
  389. return sprintf(buf, "%u\n", value & data->rdac_mask);
  390. }
  391. static ssize_t sysfs_set_reg(struct device *dev,
  392. struct device_attribute *attr,
  393. const char *buf, size_t count, u32 reg)
  394. {
  395. struct dpot_data *data = dev_get_drvdata(dev);
  396. unsigned long value;
  397. int err;
  398. if (reg & DPOT_ADDR_OTP_EN) {
  399. if (sysfs_streq(buf, "enabled"))
  400. set_bit(DPOT_RDAC_MASK & reg, data->otp_en_mask);
  401. else
  402. clear_bit(DPOT_RDAC_MASK & reg, data->otp_en_mask);
  403. return count;
  404. }
  405. if ((reg & DPOT_ADDR_OTP) &&
  406. !test_bit(DPOT_RDAC_MASK & reg, data->otp_en_mask))
  407. return -EPERM;
  408. err = kstrtoul(buf, 10, &value);
  409. if (err)
  410. return err;
  411. if (value > data->rdac_mask)
  412. value = data->rdac_mask;
  413. mutex_lock(&data->update_lock);
  414. dpot_write(data, reg, value);
  415. if (reg & DPOT_ADDR_EEPROM)
  416. msleep(26); /* Sleep while the EEPROM updates */
  417. else if (reg & DPOT_ADDR_OTP)
  418. msleep(400); /* Sleep while the OTP updates */
  419. mutex_unlock(&data->update_lock);
  420. return count;
  421. }
  422. static ssize_t sysfs_do_cmd(struct device *dev,
  423. struct device_attribute *attr,
  424. const char *buf, size_t count, u32 reg)
  425. {
  426. struct dpot_data *data = dev_get_drvdata(dev);
  427. mutex_lock(&data->update_lock);
  428. dpot_write(data, reg, 0);
  429. mutex_unlock(&data->update_lock);
  430. return count;
  431. }
  432. /* ------------------------------------------------------------------------- */
  433. #define DPOT_DEVICE_SHOW(_name, _reg) static ssize_t \
  434. show_##_name(struct device *dev, \
  435. struct device_attribute *attr, char *buf) \
  436. { \
  437. return sysfs_show_reg(dev, attr, buf, _reg); \
  438. }
  439. #define DPOT_DEVICE_SET(_name, _reg) static ssize_t \
  440. set_##_name(struct device *dev, \
  441. struct device_attribute *attr, \
  442. const char *buf, size_t count) \
  443. { \
  444. return sysfs_set_reg(dev, attr, buf, count, _reg); \
  445. }
  446. #define DPOT_DEVICE_SHOW_SET(name, reg) \
  447. DPOT_DEVICE_SHOW(name, reg) \
  448. DPOT_DEVICE_SET(name, reg) \
  449. static DEVICE_ATTR(name, S_IWUSR | S_IRUGO, show_##name, set_##name)
  450. #define DPOT_DEVICE_SHOW_ONLY(name, reg) \
  451. DPOT_DEVICE_SHOW(name, reg) \
  452. static DEVICE_ATTR(name, S_IWUSR | S_IRUGO, show_##name, NULL)
  453. DPOT_DEVICE_SHOW_SET(rdac0, DPOT_ADDR_RDAC | DPOT_RDAC0);
  454. DPOT_DEVICE_SHOW_SET(eeprom0, DPOT_ADDR_EEPROM | DPOT_RDAC0);
  455. DPOT_DEVICE_SHOW_ONLY(tolerance0, DPOT_ADDR_EEPROM | DPOT_TOL_RDAC0);
  456. DPOT_DEVICE_SHOW_SET(otp0, DPOT_ADDR_OTP | DPOT_RDAC0);
  457. DPOT_DEVICE_SHOW_SET(otp0en, DPOT_ADDR_OTP_EN | DPOT_RDAC0);
  458. DPOT_DEVICE_SHOW_SET(rdac1, DPOT_ADDR_RDAC | DPOT_RDAC1);
  459. DPOT_DEVICE_SHOW_SET(eeprom1, DPOT_ADDR_EEPROM | DPOT_RDAC1);
  460. DPOT_DEVICE_SHOW_ONLY(tolerance1, DPOT_ADDR_EEPROM | DPOT_TOL_RDAC1);
  461. DPOT_DEVICE_SHOW_SET(otp1, DPOT_ADDR_OTP | DPOT_RDAC1);
  462. DPOT_DEVICE_SHOW_SET(otp1en, DPOT_ADDR_OTP_EN | DPOT_RDAC1);
  463. DPOT_DEVICE_SHOW_SET(rdac2, DPOT_ADDR_RDAC | DPOT_RDAC2);
  464. DPOT_DEVICE_SHOW_SET(eeprom2, DPOT_ADDR_EEPROM | DPOT_RDAC2);
  465. DPOT_DEVICE_SHOW_ONLY(tolerance2, DPOT_ADDR_EEPROM | DPOT_TOL_RDAC2);
  466. DPOT_DEVICE_SHOW_SET(otp2, DPOT_ADDR_OTP | DPOT_RDAC2);
  467. DPOT_DEVICE_SHOW_SET(otp2en, DPOT_ADDR_OTP_EN | DPOT_RDAC2);
  468. DPOT_DEVICE_SHOW_SET(rdac3, DPOT_ADDR_RDAC | DPOT_RDAC3);
  469. DPOT_DEVICE_SHOW_SET(eeprom3, DPOT_ADDR_EEPROM | DPOT_RDAC3);
  470. DPOT_DEVICE_SHOW_ONLY(tolerance3, DPOT_ADDR_EEPROM | DPOT_TOL_RDAC3);
  471. DPOT_DEVICE_SHOW_SET(otp3, DPOT_ADDR_OTP | DPOT_RDAC3);
  472. DPOT_DEVICE_SHOW_SET(otp3en, DPOT_ADDR_OTP_EN | DPOT_RDAC3);
  473. DPOT_DEVICE_SHOW_SET(rdac4, DPOT_ADDR_RDAC | DPOT_RDAC4);
  474. DPOT_DEVICE_SHOW_SET(eeprom4, DPOT_ADDR_EEPROM | DPOT_RDAC4);
  475. DPOT_DEVICE_SHOW_ONLY(tolerance4, DPOT_ADDR_EEPROM | DPOT_TOL_RDAC4);
  476. DPOT_DEVICE_SHOW_SET(otp4, DPOT_ADDR_OTP | DPOT_RDAC4);
  477. DPOT_DEVICE_SHOW_SET(otp4en, DPOT_ADDR_OTP_EN | DPOT_RDAC4);
  478. DPOT_DEVICE_SHOW_SET(rdac5, DPOT_ADDR_RDAC | DPOT_RDAC5);
  479. DPOT_DEVICE_SHOW_SET(eeprom5, DPOT_ADDR_EEPROM | DPOT_RDAC5);
  480. DPOT_DEVICE_SHOW_ONLY(tolerance5, DPOT_ADDR_EEPROM | DPOT_TOL_RDAC5);
  481. DPOT_DEVICE_SHOW_SET(otp5, DPOT_ADDR_OTP | DPOT_RDAC5);
  482. DPOT_DEVICE_SHOW_SET(otp5en, DPOT_ADDR_OTP_EN | DPOT_RDAC5);
  483. static const struct attribute *dpot_attrib_wipers[] = {
  484. &dev_attr_rdac0.attr,
  485. &dev_attr_rdac1.attr,
  486. &dev_attr_rdac2.attr,
  487. &dev_attr_rdac3.attr,
  488. &dev_attr_rdac4.attr,
  489. &dev_attr_rdac5.attr,
  490. NULL
  491. };
  492. static const struct attribute *dpot_attrib_eeprom[] = {
  493. &dev_attr_eeprom0.attr,
  494. &dev_attr_eeprom1.attr,
  495. &dev_attr_eeprom2.attr,
  496. &dev_attr_eeprom3.attr,
  497. &dev_attr_eeprom4.attr,
  498. &dev_attr_eeprom5.attr,
  499. NULL
  500. };
  501. static const struct attribute *dpot_attrib_otp[] = {
  502. &dev_attr_otp0.attr,
  503. &dev_attr_otp1.attr,
  504. &dev_attr_otp2.attr,
  505. &dev_attr_otp3.attr,
  506. &dev_attr_otp4.attr,
  507. &dev_attr_otp5.attr,
  508. NULL
  509. };
  510. static const struct attribute *dpot_attrib_otp_en[] = {
  511. &dev_attr_otp0en.attr,
  512. &dev_attr_otp1en.attr,
  513. &dev_attr_otp2en.attr,
  514. &dev_attr_otp3en.attr,
  515. &dev_attr_otp4en.attr,
  516. &dev_attr_otp5en.attr,
  517. NULL
  518. };
  519. static const struct attribute *dpot_attrib_tolerance[] = {
  520. &dev_attr_tolerance0.attr,
  521. &dev_attr_tolerance1.attr,
  522. &dev_attr_tolerance2.attr,
  523. &dev_attr_tolerance3.attr,
  524. &dev_attr_tolerance4.attr,
  525. &dev_attr_tolerance5.attr,
  526. NULL
  527. };
  528. /* ------------------------------------------------------------------------- */
  529. #define DPOT_DEVICE_DO_CMD(_name, _cmd) static ssize_t \
  530. set_##_name(struct device *dev, \
  531. struct device_attribute *attr, \
  532. const char *buf, size_t count) \
  533. { \
  534. return sysfs_do_cmd(dev, attr, buf, count, _cmd); \
  535. } \
  536. static DEVICE_ATTR(_name, S_IWUSR | S_IRUGO, NULL, set_##_name)
  537. DPOT_DEVICE_DO_CMD(inc_all, DPOT_INC_ALL);
  538. DPOT_DEVICE_DO_CMD(dec_all, DPOT_DEC_ALL);
  539. DPOT_DEVICE_DO_CMD(inc_all_6db, DPOT_INC_ALL_6DB);
  540. DPOT_DEVICE_DO_CMD(dec_all_6db, DPOT_DEC_ALL_6DB);
  541. static struct attribute *ad525x_attributes_commands[] = {
  542. &dev_attr_inc_all.attr,
  543. &dev_attr_dec_all.attr,
  544. &dev_attr_inc_all_6db.attr,
  545. &dev_attr_dec_all_6db.attr,
  546. NULL
  547. };
  548. static const struct attribute_group ad525x_group_commands = {
  549. .attrs = ad525x_attributes_commands,
  550. };
  551. static int ad_dpot_add_files(struct device *dev,
  552. unsigned int features, unsigned int rdac)
  553. {
  554. int err = sysfs_create_file(&dev->kobj,
  555. dpot_attrib_wipers[rdac]);
  556. if (features & F_CMD_EEP)
  557. err |= sysfs_create_file(&dev->kobj,
  558. dpot_attrib_eeprom[rdac]);
  559. if (features & F_CMD_TOL)
  560. err |= sysfs_create_file(&dev->kobj,
  561. dpot_attrib_tolerance[rdac]);
  562. if (features & F_CMD_OTP) {
  563. err |= sysfs_create_file(&dev->kobj,
  564. dpot_attrib_otp_en[rdac]);
  565. err |= sysfs_create_file(&dev->kobj,
  566. dpot_attrib_otp[rdac]);
  567. }
  568. if (err)
  569. dev_err(dev, "failed to register sysfs hooks for RDAC%d\n",
  570. rdac);
  571. return err;
  572. }
  573. static inline void ad_dpot_remove_files(struct device *dev,
  574. unsigned int features, unsigned int rdac)
  575. {
  576. sysfs_remove_file(&dev->kobj,
  577. dpot_attrib_wipers[rdac]);
  578. if (features & F_CMD_EEP)
  579. sysfs_remove_file(&dev->kobj,
  580. dpot_attrib_eeprom[rdac]);
  581. if (features & F_CMD_TOL)
  582. sysfs_remove_file(&dev->kobj,
  583. dpot_attrib_tolerance[rdac]);
  584. if (features & F_CMD_OTP) {
  585. sysfs_remove_file(&dev->kobj,
  586. dpot_attrib_otp_en[rdac]);
  587. sysfs_remove_file(&dev->kobj,
  588. dpot_attrib_otp[rdac]);
  589. }
  590. }
  591. int ad_dpot_probe(struct device *dev,
  592. struct ad_dpot_bus_data *bdata, unsigned long devid,
  593. const char *name)
  594. {
  595. struct dpot_data *data;
  596. int i, err = 0;
  597. data = kzalloc_obj(struct dpot_data);
  598. if (!data) {
  599. err = -ENOMEM;
  600. goto exit;
  601. }
  602. dev_set_drvdata(dev, data);
  603. mutex_init(&data->update_lock);
  604. data->bdata = *bdata;
  605. data->devid = devid;
  606. data->max_pos = 1 << DPOT_MAX_POS(devid);
  607. data->rdac_mask = data->max_pos - 1;
  608. data->feat = DPOT_FEAT(devid);
  609. data->uid = DPOT_UID(devid);
  610. data->wipers = DPOT_WIPERS(devid);
  611. for (i = DPOT_RDAC0; i < MAX_RDACS; i++)
  612. if (data->wipers & (1 << i)) {
  613. err = ad_dpot_add_files(dev, data->feat, i);
  614. if (err)
  615. goto exit_remove_files;
  616. /* power-up midscale */
  617. if (data->feat & F_RDACS_WONLY)
  618. data->rdac_cache[i] = data->max_pos / 2;
  619. }
  620. if (data->feat & F_CMD_INC)
  621. err = sysfs_create_group(&dev->kobj, &ad525x_group_commands);
  622. if (err) {
  623. dev_err(dev, "failed to register sysfs hooks\n");
  624. goto exit_free;
  625. }
  626. dev_info(dev, "%s %d-Position Digital Potentiometer registered\n",
  627. name, data->max_pos);
  628. return 0;
  629. exit_remove_files:
  630. for (i = DPOT_RDAC0; i < MAX_RDACS; i++)
  631. if (data->wipers & (1 << i))
  632. ad_dpot_remove_files(dev, data->feat, i);
  633. exit_free:
  634. kfree(data);
  635. dev_set_drvdata(dev, NULL);
  636. exit:
  637. dev_err(dev, "failed to create client for %s ID 0x%lX\n",
  638. name, devid);
  639. return err;
  640. }
  641. EXPORT_SYMBOL(ad_dpot_probe);
  642. void ad_dpot_remove(struct device *dev)
  643. {
  644. struct dpot_data *data = dev_get_drvdata(dev);
  645. int i;
  646. for (i = DPOT_RDAC0; i < MAX_RDACS; i++)
  647. if (data->wipers & (1 << i))
  648. ad_dpot_remove_files(dev, data->feat, i);
  649. kfree(data);
  650. }
  651. EXPORT_SYMBOL(ad_dpot_remove);
  652. MODULE_AUTHOR("Chris Verges <chrisv@cyberswitching.com>, "
  653. "Michael Hennerich <michael.hennerich@analog.com>");
  654. MODULE_DESCRIPTION("Digital potentiometer driver");
  655. MODULE_LICENSE("GPL");