ad525x_dpot.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. /*
  2. * ad525x_dpot: Driver for the Analog Devices digital potentiometers
  3. * Copyright (c) 2009-2010 Analog Devices, Inc.
  4. * Author: Michael Hennerich <hennerich@blackfin.uclinux.org>
  5. *
  6. * DEVID #Wipers #Positions Resistor Options (kOhm)
  7. * AD5258 1 64 1, 10, 50, 100
  8. * AD5259 1 256 5, 10, 50, 100
  9. * AD5251 2 64 1, 10, 50, 100
  10. * AD5252 2 256 1, 10, 50, 100
  11. * AD5255 3 512 25, 250
  12. * AD5253 4 64 1, 10, 50, 100
  13. * AD5254 4 256 1, 10, 50, 100
  14. * AD5160 1 256 5, 10, 50, 100
  15. * AD5161 1 256 5, 10, 50, 100
  16. * AD5162 2 256 2.5, 10, 50, 100
  17. * AD5165 1 256 100
  18. * AD5200 1 256 10, 50
  19. * AD5201 1 33 10, 50
  20. * AD5203 4 64 10, 100
  21. * AD5204 4 256 10, 50, 100
  22. * AD5206 6 256 10, 50, 100
  23. * AD5207 2 256 10, 50, 100
  24. * AD5231 1 1024 10, 50, 100
  25. * AD5232 2 256 10, 50, 100
  26. * AD5233 4 64 10, 50, 100
  27. * AD5235 2 1024 25, 250
  28. * AD5260 1 256 20, 50, 200
  29. * AD5262 2 256 20, 50, 200
  30. * AD5263 4 256 20, 50, 200
  31. * AD5290 1 256 10, 50, 100
  32. * AD5291 1 256 20, 50, 100 (20-TP)
  33. * AD5292 1 1024 20, 50, 100 (20-TP)
  34. * AD5293 1 1024 20, 50, 100
  35. * AD7376 1 128 10, 50, 100, 1M
  36. * AD8400 1 256 1, 10, 50, 100
  37. * AD8402 2 256 1, 10, 50, 100
  38. * AD8403 4 256 1, 10, 50, 100
  39. * ADN2850 3 512 25, 250
  40. * AD5241 1 256 10, 100, 1M
  41. * AD5246 1 128 5, 10, 50, 100
  42. * AD5247 1 128 5, 10, 50, 100
  43. * AD5245 1 256 5, 10, 50, 100
  44. * AD5243 2 256 2.5, 10, 50, 100
  45. * AD5248 2 256 2.5, 10, 50, 100
  46. * AD5242 2 256 20, 50, 200
  47. * AD5280 1 256 20, 50, 200
  48. * AD5282 2 256 20, 50, 200
  49. * ADN2860 3 512 25, 250
  50. * AD5273 1 64 1, 10, 50, 100 (OTP)
  51. * AD5171 1 64 5, 10, 50, 100 (OTP)
  52. * AD5170 1 256 2.5, 10, 50, 100 (OTP)
  53. * AD5172 2 256 2.5, 10, 50, 100 (OTP)
  54. * AD5173 2 256 2.5, 10, 50, 100 (OTP)
  55. * AD5270 1 1024 20, 50, 100 (50-TP)
  56. * AD5271 1 256 20, 50, 100 (50-TP)
  57. * AD5272 1 1024 20, 50, 100 (50-TP)
  58. * AD5274 1 256 20, 50, 100 (50-TP)
  59. *
  60. * See Documentation/misc-devices/ad525x_dpot.txt for more info.
  61. *
  62. * derived from ad5258.c
  63. * Copyright (c) 2009 Cyber Switching, Inc.
  64. * Author: Chris Verges <chrisv@cyberswitching.com>
  65. *
  66. * derived from ad5252.c
  67. * Copyright (c) 2006-2011 Michael Hennerich <hennerich@blackfin.uclinux.org>
  68. *
  69. * Licensed under the GPL-2 or later.
  70. */
  71. #include <linux/module.h>
  72. #include <linux/device.h>
  73. #include <linux/kernel.h>
  74. #include <linux/delay.h>
  75. #include <linux/slab.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 rdac_mask;
  84. unsigned max_pos;
  85. unsigned long devid;
  86. unsigned uid;
  87. unsigned feat;
  88. unsigned 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 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 (dpot->uid == DPOT_UID(AD5291_ID))
  129. value = value >> 2;
  130. return value;
  131. } else if (dpot->uid == DPOT_UID(AD5270_ID) ||
  132. dpot->uid == DPOT_UID(AD5271_ID)) {
  133. value = dpot_read_r8d8(dpot,
  134. DPOT_AD5270_1_2_4_READ_RDAC << 2);
  135. if (value < 0)
  136. return value;
  137. if (dpot->uid == DPOT_UID(AD5271_ID))
  138. value = value >> 2;
  139. return value;
  140. }
  141. ctrl = DPOT_SPI_READ_RDAC;
  142. } else if (reg & DPOT_ADDR_EEPROM) {
  143. ctrl = DPOT_SPI_READ_EEPROM;
  144. }
  145. if (dpot->feat & F_SPI_16BIT)
  146. return dpot_read_r8d8(dpot, ctrl);
  147. else if (dpot->feat & F_SPI_24BIT)
  148. return dpot_read_r8d16(dpot, ctrl);
  149. return -EFAULT;
  150. }
  151. static s32 dpot_read_i2c(struct dpot_data *dpot, u8 reg)
  152. {
  153. int value;
  154. unsigned ctrl = 0;
  155. switch (dpot->uid) {
  156. case DPOT_UID(AD5246_ID):
  157. case DPOT_UID(AD5247_ID):
  158. return dpot_read_d8(dpot);
  159. case DPOT_UID(AD5245_ID):
  160. case DPOT_UID(AD5241_ID):
  161. case DPOT_UID(AD5242_ID):
  162. case DPOT_UID(AD5243_ID):
  163. case DPOT_UID(AD5248_ID):
  164. case DPOT_UID(AD5280_ID):
  165. case DPOT_UID(AD5282_ID):
  166. ctrl = ((reg & DPOT_RDAC_MASK) == DPOT_RDAC0) ?
  167. 0 : DPOT_AD5282_RDAC_AB;
  168. return dpot_read_r8d8(dpot, ctrl);
  169. case DPOT_UID(AD5170_ID):
  170. case DPOT_UID(AD5171_ID):
  171. case DPOT_UID(AD5273_ID):
  172. return dpot_read_d8(dpot);
  173. case DPOT_UID(AD5172_ID):
  174. case DPOT_UID(AD5173_ID):
  175. ctrl = ((reg & DPOT_RDAC_MASK) == DPOT_RDAC0) ?
  176. 0 : DPOT_AD5172_3_A0;
  177. return dpot_read_r8d8(dpot, ctrl);
  178. case DPOT_UID(AD5272_ID):
  179. case DPOT_UID(AD5274_ID):
  180. dpot_write_r8d8(dpot,
  181. (DPOT_AD5270_1_2_4_READ_RDAC << 2), 0);
  182. value = dpot_read_r8d16(dpot,
  183. DPOT_AD5270_1_2_4_RDAC << 2);
  184. if (value < 0)
  185. return value;
  186. /*
  187. * AD5272/AD5274 returns high byte first, however
  188. * underling smbus expects low byte first.
  189. */
  190. value = swab16(value);
  191. if (dpot->uid == DPOT_UID(AD5274_ID))
  192. value = value >> 2;
  193. return value;
  194. default:
  195. if ((reg & DPOT_REG_TOL) || (dpot->max_pos > 256))
  196. return dpot_read_r8d16(dpot, (reg & 0xF8) |
  197. ((reg & 0x7) << 1));
  198. else
  199. return dpot_read_r8d8(dpot, reg);
  200. }
  201. }
  202. static s32 dpot_read(struct dpot_data *dpot, u8 reg)
  203. {
  204. if (dpot->feat & F_SPI)
  205. return dpot_read_spi(dpot, reg);
  206. else
  207. return dpot_read_i2c(dpot, reg);
  208. }
  209. static s32 dpot_write_spi(struct dpot_data *dpot, u8 reg, u16 value)
  210. {
  211. unsigned val = 0;
  212. if (!(reg & (DPOT_ADDR_EEPROM | DPOT_ADDR_CMD | DPOT_ADDR_OTP))) {
  213. if (dpot->feat & F_RDACS_WONLY)
  214. dpot->rdac_cache[reg & DPOT_RDAC_MASK] = value;
  215. if (dpot->feat & F_AD_APPDATA) {
  216. if (dpot->feat & F_SPI_8BIT) {
  217. val = ((reg & DPOT_RDAC_MASK) <<
  218. DPOT_MAX_POS(dpot->devid)) |
  219. value;
  220. return dpot_write_d8(dpot, val);
  221. } else if (dpot->feat & F_SPI_16BIT) {
  222. val = ((reg & DPOT_RDAC_MASK) <<
  223. DPOT_MAX_POS(dpot->devid)) |
  224. value;
  225. return dpot_write_r8d8(dpot, val >> 8,
  226. val & 0xFF);
  227. } else
  228. BUG();
  229. } else {
  230. if (dpot->uid == DPOT_UID(AD5291_ID) ||
  231. dpot->uid == DPOT_UID(AD5292_ID) ||
  232. dpot->uid == DPOT_UID(AD5293_ID)) {
  233. dpot_write_r8d8(dpot, DPOT_AD5291_CTRLREG << 2,
  234. DPOT_AD5291_UNLOCK_CMD);
  235. if (dpot->uid == DPOT_UID(AD5291_ID))
  236. value = value << 2;
  237. return dpot_write_r8d8(dpot,
  238. (DPOT_AD5291_RDAC << 2) |
  239. (value >> 8), value & 0xFF);
  240. } else if (dpot->uid == DPOT_UID(AD5270_ID) ||
  241. dpot->uid == DPOT_UID(AD5271_ID)) {
  242. dpot_write_r8d8(dpot,
  243. DPOT_AD5270_1_2_4_CTRLREG << 2,
  244. DPOT_AD5270_1_2_4_UNLOCK_CMD);
  245. if (dpot->uid == DPOT_UID(AD5271_ID))
  246. value = value << 2;
  247. return dpot_write_r8d8(dpot,
  248. (DPOT_AD5270_1_2_4_RDAC << 2) |
  249. (value >> 8), value & 0xFF);
  250. }
  251. val = DPOT_SPI_RDAC | (reg & DPOT_RDAC_MASK);
  252. }
  253. } else if (reg & DPOT_ADDR_EEPROM) {
  254. val = DPOT_SPI_EEPROM | (reg & DPOT_RDAC_MASK);
  255. } else if (reg & DPOT_ADDR_CMD) {
  256. switch (reg) {
  257. case DPOT_DEC_ALL_6DB:
  258. val = DPOT_SPI_DEC_ALL_6DB;
  259. break;
  260. case DPOT_INC_ALL_6DB:
  261. val = DPOT_SPI_INC_ALL_6DB;
  262. break;
  263. case DPOT_DEC_ALL:
  264. val = DPOT_SPI_DEC_ALL;
  265. break;
  266. case DPOT_INC_ALL:
  267. val = DPOT_SPI_INC_ALL;
  268. break;
  269. }
  270. } else if (reg & DPOT_ADDR_OTP) {
  271. if (dpot->uid == DPOT_UID(AD5291_ID) ||
  272. dpot->uid == DPOT_UID(AD5292_ID)) {
  273. return dpot_write_r8d8(dpot,
  274. DPOT_AD5291_STORE_XTPM << 2, 0);
  275. } else if (dpot->uid == DPOT_UID(AD5270_ID) ||
  276. dpot->uid == DPOT_UID(AD5271_ID)) {
  277. return dpot_write_r8d8(dpot,
  278. DPOT_AD5270_1_2_4_STORE_XTPM << 2, 0);
  279. }
  280. } else
  281. BUG();
  282. if (dpot->feat & F_SPI_16BIT)
  283. return dpot_write_r8d8(dpot, val, value);
  284. else if (dpot->feat & F_SPI_24BIT)
  285. return dpot_write_r8d16(dpot, val, value);
  286. return -EFAULT;
  287. }
  288. static s32 dpot_write_i2c(struct dpot_data *dpot, u8 reg, u16 value)
  289. {
  290. /* Only write the instruction byte for certain commands */
  291. unsigned tmp = 0, ctrl = 0;
  292. switch (dpot->uid) {
  293. case DPOT_UID(AD5246_ID):
  294. case DPOT_UID(AD5247_ID):
  295. return dpot_write_d8(dpot, value);
  296. case DPOT_UID(AD5245_ID):
  297. case DPOT_UID(AD5241_ID):
  298. case DPOT_UID(AD5242_ID):
  299. case DPOT_UID(AD5243_ID):
  300. case DPOT_UID(AD5248_ID):
  301. case DPOT_UID(AD5280_ID):
  302. case DPOT_UID(AD5282_ID):
  303. ctrl = ((reg & DPOT_RDAC_MASK) == DPOT_RDAC0) ?
  304. 0 : DPOT_AD5282_RDAC_AB;
  305. return dpot_write_r8d8(dpot, ctrl, value);
  306. case DPOT_UID(AD5171_ID):
  307. case DPOT_UID(AD5273_ID):
  308. if (reg & DPOT_ADDR_OTP) {
  309. tmp = dpot_read_d8(dpot);
  310. if (tmp >> 6) /* Ready to Program? */
  311. return -EFAULT;
  312. ctrl = DPOT_AD5273_FUSE;
  313. }
  314. return dpot_write_r8d8(dpot, ctrl, value);
  315. case DPOT_UID(AD5172_ID):
  316. case DPOT_UID(AD5173_ID):
  317. ctrl = ((reg & DPOT_RDAC_MASK) == DPOT_RDAC0) ?
  318. 0 : DPOT_AD5172_3_A0;
  319. if (reg & DPOT_ADDR_OTP) {
  320. tmp = dpot_read_r8d16(dpot, ctrl);
  321. if (tmp >> 14) /* Ready to Program? */
  322. return -EFAULT;
  323. ctrl |= DPOT_AD5170_2_3_FUSE;
  324. }
  325. return dpot_write_r8d8(dpot, ctrl, value);
  326. case DPOT_UID(AD5170_ID):
  327. if (reg & DPOT_ADDR_OTP) {
  328. tmp = dpot_read_r8d16(dpot, tmp);
  329. if (tmp >> 14) /* Ready to Program? */
  330. return -EFAULT;
  331. ctrl = DPOT_AD5170_2_3_FUSE;
  332. }
  333. return dpot_write_r8d8(dpot, ctrl, value);
  334. case DPOT_UID(AD5272_ID):
  335. case DPOT_UID(AD5274_ID):
  336. dpot_write_r8d8(dpot, DPOT_AD5270_1_2_4_CTRLREG << 2,
  337. DPOT_AD5270_1_2_4_UNLOCK_CMD);
  338. if (reg & DPOT_ADDR_OTP)
  339. return dpot_write_r8d8(dpot,
  340. DPOT_AD5270_1_2_4_STORE_XTPM << 2, 0);
  341. if (dpot->uid == DPOT_UID(AD5274_ID))
  342. value = value << 2;
  343. return dpot_write_r8d8(dpot, (DPOT_AD5270_1_2_4_RDAC << 2) |
  344. (value >> 8), value & 0xFF);
  345. default:
  346. if (reg & DPOT_ADDR_CMD)
  347. return dpot_write_d8(dpot, reg);
  348. if (dpot->max_pos > 256)
  349. return dpot_write_r8d16(dpot, (reg & 0xF8) |
  350. ((reg & 0x7) << 1), value);
  351. else
  352. /* All other registers require instruction + data bytes */
  353. return dpot_write_r8d8(dpot, reg, value);
  354. }
  355. }
  356. static s32 dpot_write(struct dpot_data *dpot, u8 reg, u16 value)
  357. {
  358. if (dpot->feat & F_SPI)
  359. return dpot_write_spi(dpot, reg, value);
  360. else
  361. return dpot_write_i2c(dpot, reg, value);
  362. }
  363. /* sysfs functions */
  364. static ssize_t sysfs_show_reg(struct device *dev,
  365. struct device_attribute *attr,
  366. char *buf, u32 reg)
  367. {
  368. struct dpot_data *data = dev_get_drvdata(dev);
  369. s32 value;
  370. if (reg & DPOT_ADDR_OTP_EN)
  371. return sprintf(buf, "%s\n",
  372. test_bit(DPOT_RDAC_MASK & reg, data->otp_en_mask) ?
  373. "enabled" : "disabled");
  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 (!strncmp(buf, "enabled", sizeof("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 features, unsigned 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 features, unsigned 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(sizeof(struct dpot_data), GFP_KERNEL);
  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. int 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. return 0;
  651. }
  652. EXPORT_SYMBOL(ad_dpot_remove);
  653. MODULE_AUTHOR("Chris Verges <chrisv@cyberswitching.com>, "
  654. "Michael Hennerich <hennerich@blackfin.uclinux.org>");
  655. MODULE_DESCRIPTION("Digital potentiometer driver");
  656. MODULE_LICENSE("GPL");