max1363.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702
  1. /*
  2. * iio/adc/max1363.c
  3. * Copyright (C) 2008-2010 Jonathan Cameron
  4. *
  5. * based on linux/drivers/i2c/chips/max123x
  6. * Copyright (C) 2002-2004 Stefan Eletzhofer
  7. *
  8. * based on linux/drivers/acron/char/pcf8583.c
  9. * Copyright (C) 2000 Russell King
  10. *
  11. * Driver for max1363 and similar chips.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2 as
  15. * published by the Free Software Foundation.
  16. */
  17. #include <linux/interrupt.h>
  18. #include <linux/device.h>
  19. #include <linux/kernel.h>
  20. #include <linux/sysfs.h>
  21. #include <linux/list.h>
  22. #include <linux/i2c.h>
  23. #include <linux/regulator/consumer.h>
  24. #include <linux/slab.h>
  25. #include <linux/err.h>
  26. #include <linux/module.h>
  27. #include <linux/iio/iio.h>
  28. #include <linux/iio/sysfs.h>
  29. #include <linux/iio/events.h>
  30. #include <linux/iio/buffer.h>
  31. #include <linux/iio/driver.h>
  32. #include <linux/iio/kfifo_buf.h>
  33. #include <linux/iio/trigger_consumer.h>
  34. #include <linux/iio/triggered_buffer.h>
  35. #define MAX1363_SETUP_BYTE(a) ((a) | 0x80)
  36. /* There is a fair bit more defined here than currently
  37. * used, but the intention is to support everything these
  38. * chips do in the long run */
  39. /* see data sheets */
  40. /* max1363 and max1236, max1237, max1238, max1239 */
  41. #define MAX1363_SETUP_AIN3_IS_AIN3_REF_IS_VDD 0x00
  42. #define MAX1363_SETUP_AIN3_IS_REF_EXT_TO_REF 0x20
  43. #define MAX1363_SETUP_AIN3_IS_AIN3_REF_IS_INT 0x40
  44. #define MAX1363_SETUP_AIN3_IS_REF_REF_IS_INT 0x60
  45. #define MAX1363_SETUP_POWER_UP_INT_REF 0x10
  46. #define MAX1363_SETUP_POWER_DOWN_INT_REF 0x00
  47. /* think about including max11600 etc - more settings */
  48. #define MAX1363_SETUP_EXT_CLOCK 0x08
  49. #define MAX1363_SETUP_INT_CLOCK 0x00
  50. #define MAX1363_SETUP_UNIPOLAR 0x00
  51. #define MAX1363_SETUP_BIPOLAR 0x04
  52. #define MAX1363_SETUP_RESET 0x00
  53. #define MAX1363_SETUP_NORESET 0x02
  54. /* max1363 only - though don't care on others.
  55. * For now monitor modes are not implemented as the relevant
  56. * line is not connected on my test board.
  57. * The definitions are here as I intend to add this soon.
  58. */
  59. #define MAX1363_SETUP_MONITOR_SETUP 0x01
  60. /* Specific to the max1363 */
  61. #define MAX1363_MON_RESET_CHAN(a) (1 << ((a) + 4))
  62. #define MAX1363_MON_INT_ENABLE 0x01
  63. /* defined for readability reasons */
  64. /* All chips */
  65. #define MAX1363_CONFIG_BYTE(a) ((a))
  66. #define MAX1363_CONFIG_SE 0x01
  67. #define MAX1363_CONFIG_DE 0x00
  68. #define MAX1363_CONFIG_SCAN_TO_CS 0x00
  69. #define MAX1363_CONFIG_SCAN_SINGLE_8 0x20
  70. #define MAX1363_CONFIG_SCAN_MONITOR_MODE 0x40
  71. #define MAX1363_CONFIG_SCAN_SINGLE_1 0x60
  72. /* max123{6-9} only */
  73. #define MAX1236_SCAN_MID_TO_CHANNEL 0x40
  74. /* max1363 only - merely part of channel selects or don't care for others */
  75. #define MAX1363_CONFIG_EN_MON_MODE_READ 0x18
  76. #define MAX1363_CHANNEL_SEL(a) ((a) << 1)
  77. /* max1363 strictly 0x06 - but doesn't matter */
  78. #define MAX1363_CHANNEL_SEL_MASK 0x1E
  79. #define MAX1363_SCAN_MASK 0x60
  80. #define MAX1363_SE_DE_MASK 0x01
  81. #define MAX1363_MAX_CHANNELS 25
  82. /**
  83. * struct max1363_mode - scan mode information
  84. * @conf: The corresponding value of the configuration register
  85. * @modemask: Bit mask corresponding to channels enabled in this mode
  86. */
  87. struct max1363_mode {
  88. int8_t conf;
  89. DECLARE_BITMAP(modemask, MAX1363_MAX_CHANNELS);
  90. };
  91. /* This must be maintained along side the max1363_mode_table in max1363_core */
  92. enum max1363_modes {
  93. /* Single read of a single channel */
  94. _s0, _s1, _s2, _s3, _s4, _s5, _s6, _s7, _s8, _s9, _s10, _s11,
  95. /* Differential single read */
  96. d0m1, d2m3, d4m5, d6m7, d8m9, d10m11,
  97. d1m0, d3m2, d5m4, d7m6, d9m8, d11m10,
  98. /* Scan to channel and mid to channel where overlapping */
  99. s0to1, s0to2, s2to3, s0to3, s0to4, s0to5, s0to6,
  100. s6to7, s0to7, s6to8, s0to8, s6to9,
  101. s0to9, s6to10, s0to10, s6to11, s0to11,
  102. /* Differential scan to channel and mid to channel where overlapping */
  103. d0m1to2m3, d0m1to4m5, d0m1to6m7, d6m7to8m9,
  104. d0m1to8m9, d6m7to10m11, d0m1to10m11, d1m0to3m2,
  105. d1m0to5m4, d1m0to7m6, d7m6to9m8, d1m0to9m8,
  106. d7m6to11m10, d1m0to11m10,
  107. };
  108. /**
  109. * struct max1363_chip_info - chip specifc information
  110. * @info: iio core function callbacks structure
  111. * @channels: channel specification
  112. * @num_channels: number of channels
  113. * @mode_list: array of available scan modes
  114. * @default_mode: the scan mode in which the chip starts up
  115. * @int_vref_mv: the internal reference voltage
  116. * @num_modes: number of modes
  117. * @bits: accuracy of the adc in bits
  118. */
  119. struct max1363_chip_info {
  120. const struct iio_info *info;
  121. const struct iio_chan_spec *channels;
  122. int num_channels;
  123. const enum max1363_modes *mode_list;
  124. enum max1363_modes default_mode;
  125. u16 int_vref_mv;
  126. u8 num_modes;
  127. u8 bits;
  128. };
  129. /**
  130. * struct max1363_state - driver instance specific data
  131. * @client: i2c_client
  132. * @setupbyte: cache of current device setup byte
  133. * @configbyte: cache of current device config byte
  134. * @chip_info: chip model specific constants, available modes, etc.
  135. * @current_mode: the scan mode of this chip
  136. * @requestedmask: a valid requested set of channels
  137. * @reg: supply regulator
  138. * @monitor_on: whether monitor mode is enabled
  139. * @monitor_speed: parameter corresponding to device monitor speed setting
  140. * @mask_high: bitmask for enabled high thresholds
  141. * @mask_low: bitmask for enabled low thresholds
  142. * @thresh_high: high threshold values
  143. * @thresh_low: low threshold values
  144. * @vref: Reference voltage regulator
  145. * @vref_uv: Actual (external or internal) reference voltage
  146. * @send: function used to send data to the chip
  147. * @recv: function used to receive data from the chip
  148. */
  149. struct max1363_state {
  150. struct i2c_client *client;
  151. u8 setupbyte;
  152. u8 configbyte;
  153. const struct max1363_chip_info *chip_info;
  154. const struct max1363_mode *current_mode;
  155. u32 requestedmask;
  156. struct regulator *reg;
  157. /* Using monitor modes and buffer at the same time is
  158. currently not supported */
  159. bool monitor_on;
  160. unsigned int monitor_speed:3;
  161. u8 mask_high;
  162. u8 mask_low;
  163. /* 4x unipolar first then the fours bipolar ones */
  164. s16 thresh_high[8];
  165. s16 thresh_low[8];
  166. struct regulator *vref;
  167. u32 vref_uv;
  168. int (*send)(const struct i2c_client *client,
  169. const char *buf, int count);
  170. int (*recv)(const struct i2c_client *client,
  171. char *buf, int count);
  172. };
  173. #define MAX1363_MODE_SINGLE(_num, _mask) { \
  174. .conf = MAX1363_CHANNEL_SEL(_num) \
  175. | MAX1363_CONFIG_SCAN_SINGLE_1 \
  176. | MAX1363_CONFIG_SE, \
  177. .modemask[0] = _mask, \
  178. }
  179. #define MAX1363_MODE_SCAN_TO_CHANNEL(_num, _mask) { \
  180. .conf = MAX1363_CHANNEL_SEL(_num) \
  181. | MAX1363_CONFIG_SCAN_TO_CS \
  182. | MAX1363_CONFIG_SE, \
  183. .modemask[0] = _mask, \
  184. }
  185. /* note not available for max1363 hence naming */
  186. #define MAX1236_MODE_SCAN_MID_TO_CHANNEL(_mid, _num, _mask) { \
  187. .conf = MAX1363_CHANNEL_SEL(_num) \
  188. | MAX1236_SCAN_MID_TO_CHANNEL \
  189. | MAX1363_CONFIG_SE, \
  190. .modemask[0] = _mask \
  191. }
  192. #define MAX1363_MODE_DIFF_SINGLE(_nump, _numm, _mask) { \
  193. .conf = MAX1363_CHANNEL_SEL(_nump) \
  194. | MAX1363_CONFIG_SCAN_SINGLE_1 \
  195. | MAX1363_CONFIG_DE, \
  196. .modemask[0] = _mask \
  197. }
  198. /* Can't think how to automate naming so specify for now */
  199. #define MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(_num, _numvals, _mask) { \
  200. .conf = MAX1363_CHANNEL_SEL(_num) \
  201. | MAX1363_CONFIG_SCAN_TO_CS \
  202. | MAX1363_CONFIG_DE, \
  203. .modemask[0] = _mask \
  204. }
  205. /* note only available for max1363 hence naming */
  206. #define MAX1236_MODE_DIFF_SCAN_MID_TO_CHANNEL(_num, _numvals, _mask) { \
  207. .conf = MAX1363_CHANNEL_SEL(_num) \
  208. | MAX1236_SCAN_MID_TO_CHANNEL \
  209. | MAX1363_CONFIG_SE, \
  210. .modemask[0] = _mask \
  211. }
  212. static const struct max1363_mode max1363_mode_table[] = {
  213. /* All of the single channel options first */
  214. MAX1363_MODE_SINGLE(0, 1 << 0),
  215. MAX1363_MODE_SINGLE(1, 1 << 1),
  216. MAX1363_MODE_SINGLE(2, 1 << 2),
  217. MAX1363_MODE_SINGLE(3, 1 << 3),
  218. MAX1363_MODE_SINGLE(4, 1 << 4),
  219. MAX1363_MODE_SINGLE(5, 1 << 5),
  220. MAX1363_MODE_SINGLE(6, 1 << 6),
  221. MAX1363_MODE_SINGLE(7, 1 << 7),
  222. MAX1363_MODE_SINGLE(8, 1 << 8),
  223. MAX1363_MODE_SINGLE(9, 1 << 9),
  224. MAX1363_MODE_SINGLE(10, 1 << 10),
  225. MAX1363_MODE_SINGLE(11, 1 << 11),
  226. MAX1363_MODE_DIFF_SINGLE(0, 1, 1 << 12),
  227. MAX1363_MODE_DIFF_SINGLE(2, 3, 1 << 13),
  228. MAX1363_MODE_DIFF_SINGLE(4, 5, 1 << 14),
  229. MAX1363_MODE_DIFF_SINGLE(6, 7, 1 << 15),
  230. MAX1363_MODE_DIFF_SINGLE(8, 9, 1 << 16),
  231. MAX1363_MODE_DIFF_SINGLE(10, 11, 1 << 17),
  232. MAX1363_MODE_DIFF_SINGLE(1, 0, 1 << 18),
  233. MAX1363_MODE_DIFF_SINGLE(3, 2, 1 << 19),
  234. MAX1363_MODE_DIFF_SINGLE(5, 4, 1 << 20),
  235. MAX1363_MODE_DIFF_SINGLE(7, 6, 1 << 21),
  236. MAX1363_MODE_DIFF_SINGLE(9, 8, 1 << 22),
  237. MAX1363_MODE_DIFF_SINGLE(11, 10, 1 << 23),
  238. /* The multichannel scans next */
  239. MAX1363_MODE_SCAN_TO_CHANNEL(1, 0x003),
  240. MAX1363_MODE_SCAN_TO_CHANNEL(2, 0x007),
  241. MAX1236_MODE_SCAN_MID_TO_CHANNEL(2, 3, 0x00C),
  242. MAX1363_MODE_SCAN_TO_CHANNEL(3, 0x00F),
  243. MAX1363_MODE_SCAN_TO_CHANNEL(4, 0x01F),
  244. MAX1363_MODE_SCAN_TO_CHANNEL(5, 0x03F),
  245. MAX1363_MODE_SCAN_TO_CHANNEL(6, 0x07F),
  246. MAX1236_MODE_SCAN_MID_TO_CHANNEL(6, 7, 0x0C0),
  247. MAX1363_MODE_SCAN_TO_CHANNEL(7, 0x0FF),
  248. MAX1236_MODE_SCAN_MID_TO_CHANNEL(6, 8, 0x1C0),
  249. MAX1363_MODE_SCAN_TO_CHANNEL(8, 0x1FF),
  250. MAX1236_MODE_SCAN_MID_TO_CHANNEL(6, 9, 0x3C0),
  251. MAX1363_MODE_SCAN_TO_CHANNEL(9, 0x3FF),
  252. MAX1236_MODE_SCAN_MID_TO_CHANNEL(6, 10, 0x7C0),
  253. MAX1363_MODE_SCAN_TO_CHANNEL(10, 0x7FF),
  254. MAX1236_MODE_SCAN_MID_TO_CHANNEL(6, 11, 0xFC0),
  255. MAX1363_MODE_SCAN_TO_CHANNEL(11, 0xFFF),
  256. MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(2, 2, 0x003000),
  257. MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(4, 3, 0x007000),
  258. MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(6, 4, 0x00F000),
  259. MAX1236_MODE_DIFF_SCAN_MID_TO_CHANNEL(8, 2, 0x018000),
  260. MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(8, 5, 0x01F000),
  261. MAX1236_MODE_DIFF_SCAN_MID_TO_CHANNEL(10, 3, 0x038000),
  262. MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(10, 6, 0x3F000),
  263. MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(3, 2, 0x0C0000),
  264. MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(5, 3, 0x1C0000),
  265. MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(7, 4, 0x3C0000),
  266. MAX1236_MODE_DIFF_SCAN_MID_TO_CHANNEL(9, 2, 0x600000),
  267. MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(9, 5, 0x7C0000),
  268. MAX1236_MODE_DIFF_SCAN_MID_TO_CHANNEL(11, 3, 0xE00000),
  269. MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(11, 6, 0xFC0000),
  270. };
  271. static const struct max1363_mode
  272. *max1363_match_mode(const unsigned long *mask,
  273. const struct max1363_chip_info *ci)
  274. {
  275. int i;
  276. if (mask)
  277. for (i = 0; i < ci->num_modes; i++)
  278. if (bitmap_subset(mask,
  279. max1363_mode_table[ci->mode_list[i]].
  280. modemask,
  281. MAX1363_MAX_CHANNELS))
  282. return &max1363_mode_table[ci->mode_list[i]];
  283. return NULL;
  284. }
  285. static int max1363_smbus_send(const struct i2c_client *client, const char *buf,
  286. int count)
  287. {
  288. int i, err;
  289. for (i = err = 0; err == 0 && i < count; ++i)
  290. err = i2c_smbus_write_byte(client, buf[i]);
  291. return err ? err : count;
  292. }
  293. static int max1363_smbus_recv(const struct i2c_client *client, char *buf,
  294. int count)
  295. {
  296. int i, ret;
  297. for (i = 0; i < count; ++i) {
  298. ret = i2c_smbus_read_byte(client);
  299. if (ret < 0)
  300. return ret;
  301. buf[i] = ret;
  302. }
  303. return count;
  304. }
  305. static int max1363_write_basic_config(struct max1363_state *st)
  306. {
  307. u8 tx_buf[2] = { st->setupbyte, st->configbyte };
  308. return st->send(st->client, tx_buf, 2);
  309. }
  310. static int max1363_set_scan_mode(struct max1363_state *st)
  311. {
  312. st->configbyte &= ~(MAX1363_CHANNEL_SEL_MASK
  313. | MAX1363_SCAN_MASK
  314. | MAX1363_SE_DE_MASK);
  315. st->configbyte |= st->current_mode->conf;
  316. return max1363_write_basic_config(st);
  317. }
  318. static int max1363_read_single_chan(struct iio_dev *indio_dev,
  319. struct iio_chan_spec const *chan,
  320. int *val,
  321. long m)
  322. {
  323. int ret = 0;
  324. s32 data;
  325. u8 rxbuf[2];
  326. struct max1363_state *st = iio_priv(indio_dev);
  327. struct i2c_client *client = st->client;
  328. mutex_lock(&indio_dev->mlock);
  329. /*
  330. * If monitor mode is enabled, the method for reading a single
  331. * channel will have to be rather different and has not yet
  332. * been implemented.
  333. *
  334. * Also, cannot read directly if buffered capture enabled.
  335. */
  336. if (st->monitor_on || iio_buffer_enabled(indio_dev)) {
  337. ret = -EBUSY;
  338. goto error_ret;
  339. }
  340. /* Check to see if current scan mode is correct */
  341. if (st->current_mode != &max1363_mode_table[chan->address]) {
  342. /* Update scan mode if needed */
  343. st->current_mode = &max1363_mode_table[chan->address];
  344. ret = max1363_set_scan_mode(st);
  345. if (ret < 0)
  346. goto error_ret;
  347. }
  348. if (st->chip_info->bits != 8) {
  349. /* Get reading */
  350. data = st->recv(client, rxbuf, 2);
  351. if (data < 0) {
  352. ret = data;
  353. goto error_ret;
  354. }
  355. data = (rxbuf[1] | rxbuf[0] << 8) &
  356. ((1 << st->chip_info->bits) - 1);
  357. } else {
  358. /* Get reading */
  359. data = st->recv(client, rxbuf, 1);
  360. if (data < 0) {
  361. ret = data;
  362. goto error_ret;
  363. }
  364. data = rxbuf[0];
  365. }
  366. *val = data;
  367. error_ret:
  368. mutex_unlock(&indio_dev->mlock);
  369. return ret;
  370. }
  371. static int max1363_read_raw(struct iio_dev *indio_dev,
  372. struct iio_chan_spec const *chan,
  373. int *val,
  374. int *val2,
  375. long m)
  376. {
  377. struct max1363_state *st = iio_priv(indio_dev);
  378. int ret;
  379. switch (m) {
  380. case IIO_CHAN_INFO_RAW:
  381. ret = max1363_read_single_chan(indio_dev, chan, val, m);
  382. if (ret < 0)
  383. return ret;
  384. return IIO_VAL_INT;
  385. case IIO_CHAN_INFO_SCALE:
  386. *val = st->vref_uv / 1000;
  387. *val2 = st->chip_info->bits;
  388. return IIO_VAL_FRACTIONAL_LOG2;
  389. default:
  390. return -EINVAL;
  391. }
  392. return 0;
  393. }
  394. /* Applies to max1363 */
  395. static const enum max1363_modes max1363_mode_list[] = {
  396. _s0, _s1, _s2, _s3,
  397. s0to1, s0to2, s0to3,
  398. d0m1, d2m3, d1m0, d3m2,
  399. d0m1to2m3, d1m0to3m2,
  400. };
  401. static const struct iio_event_spec max1363_events[] = {
  402. {
  403. .type = IIO_EV_TYPE_THRESH,
  404. .dir = IIO_EV_DIR_RISING,
  405. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  406. BIT(IIO_EV_INFO_ENABLE),
  407. }, {
  408. .type = IIO_EV_TYPE_THRESH,
  409. .dir = IIO_EV_DIR_FALLING,
  410. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  411. BIT(IIO_EV_INFO_ENABLE),
  412. },
  413. };
  414. #define MAX1363_CHAN_U(num, addr, si, bits, ev_spec, num_ev_spec) \
  415. { \
  416. .type = IIO_VOLTAGE, \
  417. .indexed = 1, \
  418. .channel = num, \
  419. .address = addr, \
  420. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  421. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  422. .datasheet_name = "AIN"#num, \
  423. .scan_type = { \
  424. .sign = 'u', \
  425. .realbits = bits, \
  426. .storagebits = (bits > 8) ? 16 : 8, \
  427. .endianness = IIO_BE, \
  428. }, \
  429. .scan_index = si, \
  430. .event_spec = ev_spec, \
  431. .num_event_specs = num_ev_spec, \
  432. }
  433. /* bipolar channel */
  434. #define MAX1363_CHAN_B(num, num2, addr, si, bits, ev_spec, num_ev_spec) \
  435. { \
  436. .type = IIO_VOLTAGE, \
  437. .differential = 1, \
  438. .indexed = 1, \
  439. .channel = num, \
  440. .channel2 = num2, \
  441. .address = addr, \
  442. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  443. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  444. .datasheet_name = "AIN"#num"-AIN"#num2, \
  445. .scan_type = { \
  446. .sign = 's', \
  447. .realbits = bits, \
  448. .storagebits = (bits > 8) ? 16 : 8, \
  449. .endianness = IIO_BE, \
  450. }, \
  451. .scan_index = si, \
  452. .event_spec = ev_spec, \
  453. .num_event_specs = num_ev_spec, \
  454. }
  455. #define MAX1363_4X_CHANS(bits, ev_spec, num_ev_spec) { \
  456. MAX1363_CHAN_U(0, _s0, 0, bits, ev_spec, num_ev_spec), \
  457. MAX1363_CHAN_U(1, _s1, 1, bits, ev_spec, num_ev_spec), \
  458. MAX1363_CHAN_U(2, _s2, 2, bits, ev_spec, num_ev_spec), \
  459. MAX1363_CHAN_U(3, _s3, 3, bits, ev_spec, num_ev_spec), \
  460. MAX1363_CHAN_B(0, 1, d0m1, 4, bits, ev_spec, num_ev_spec), \
  461. MAX1363_CHAN_B(2, 3, d2m3, 5, bits, ev_spec, num_ev_spec), \
  462. MAX1363_CHAN_B(1, 0, d1m0, 6, bits, ev_spec, num_ev_spec), \
  463. MAX1363_CHAN_B(3, 2, d3m2, 7, bits, ev_spec, num_ev_spec), \
  464. IIO_CHAN_SOFT_TIMESTAMP(8) \
  465. }
  466. static const struct iio_chan_spec max1036_channels[] =
  467. MAX1363_4X_CHANS(8, NULL, 0);
  468. static const struct iio_chan_spec max1136_channels[] =
  469. MAX1363_4X_CHANS(10, NULL, 0);
  470. static const struct iio_chan_spec max1236_channels[] =
  471. MAX1363_4X_CHANS(12, NULL, 0);
  472. static const struct iio_chan_spec max1361_channels[] =
  473. MAX1363_4X_CHANS(10, max1363_events, ARRAY_SIZE(max1363_events));
  474. static const struct iio_chan_spec max1363_channels[] =
  475. MAX1363_4X_CHANS(12, max1363_events, ARRAY_SIZE(max1363_events));
  476. /* Applies to max1236, max1237 */
  477. static const enum max1363_modes max1236_mode_list[] = {
  478. _s0, _s1, _s2, _s3,
  479. s0to1, s0to2, s0to3,
  480. d0m1, d2m3, d1m0, d3m2,
  481. d0m1to2m3, d1m0to3m2,
  482. s2to3,
  483. };
  484. /* Applies to max1238, max1239 */
  485. static const enum max1363_modes max1238_mode_list[] = {
  486. _s0, _s1, _s2, _s3, _s4, _s5, _s6, _s7, _s8, _s9, _s10, _s11,
  487. s0to1, s0to2, s0to3, s0to4, s0to5, s0to6,
  488. s0to7, s0to8, s0to9, s0to10, s0to11,
  489. d0m1, d2m3, d4m5, d6m7, d8m9, d10m11,
  490. d1m0, d3m2, d5m4, d7m6, d9m8, d11m10,
  491. d0m1to2m3, d0m1to4m5, d0m1to6m7, d0m1to8m9, d0m1to10m11,
  492. d1m0to3m2, d1m0to5m4, d1m0to7m6, d1m0to9m8, d1m0to11m10,
  493. s6to7, s6to8, s6to9, s6to10, s6to11,
  494. d6m7to8m9, d6m7to10m11, d7m6to9m8, d7m6to11m10,
  495. };
  496. #define MAX1363_12X_CHANS(bits) { \
  497. MAX1363_CHAN_U(0, _s0, 0, bits, NULL, 0), \
  498. MAX1363_CHAN_U(1, _s1, 1, bits, NULL, 0), \
  499. MAX1363_CHAN_U(2, _s2, 2, bits, NULL, 0), \
  500. MAX1363_CHAN_U(3, _s3, 3, bits, NULL, 0), \
  501. MAX1363_CHAN_U(4, _s4, 4, bits, NULL, 0), \
  502. MAX1363_CHAN_U(5, _s5, 5, bits, NULL, 0), \
  503. MAX1363_CHAN_U(6, _s6, 6, bits, NULL, 0), \
  504. MAX1363_CHAN_U(7, _s7, 7, bits, NULL, 0), \
  505. MAX1363_CHAN_U(8, _s8, 8, bits, NULL, 0), \
  506. MAX1363_CHAN_U(9, _s9, 9, bits, NULL, 0), \
  507. MAX1363_CHAN_U(10, _s10, 10, bits, NULL, 0), \
  508. MAX1363_CHAN_U(11, _s11, 11, bits, NULL, 0), \
  509. MAX1363_CHAN_B(0, 1, d0m1, 12, bits, NULL, 0), \
  510. MAX1363_CHAN_B(2, 3, d2m3, 13, bits, NULL, 0), \
  511. MAX1363_CHAN_B(4, 5, d4m5, 14, bits, NULL, 0), \
  512. MAX1363_CHAN_B(6, 7, d6m7, 15, bits, NULL, 0), \
  513. MAX1363_CHAN_B(8, 9, d8m9, 16, bits, NULL, 0), \
  514. MAX1363_CHAN_B(10, 11, d10m11, 17, bits, NULL, 0), \
  515. MAX1363_CHAN_B(1, 0, d1m0, 18, bits, NULL, 0), \
  516. MAX1363_CHAN_B(3, 2, d3m2, 19, bits, NULL, 0), \
  517. MAX1363_CHAN_B(5, 4, d5m4, 20, bits, NULL, 0), \
  518. MAX1363_CHAN_B(7, 6, d7m6, 21, bits, NULL, 0), \
  519. MAX1363_CHAN_B(9, 8, d9m8, 22, bits, NULL, 0), \
  520. MAX1363_CHAN_B(11, 10, d11m10, 23, bits, NULL, 0), \
  521. IIO_CHAN_SOFT_TIMESTAMP(24) \
  522. }
  523. static const struct iio_chan_spec max1038_channels[] = MAX1363_12X_CHANS(8);
  524. static const struct iio_chan_spec max1138_channels[] = MAX1363_12X_CHANS(10);
  525. static const struct iio_chan_spec max1238_channels[] = MAX1363_12X_CHANS(12);
  526. static const enum max1363_modes max11607_mode_list[] = {
  527. _s0, _s1, _s2, _s3,
  528. s0to1, s0to2, s0to3,
  529. s2to3,
  530. d0m1, d2m3, d1m0, d3m2,
  531. d0m1to2m3, d1m0to3m2,
  532. };
  533. static const enum max1363_modes max11608_mode_list[] = {
  534. _s0, _s1, _s2, _s3, _s4, _s5, _s6, _s7,
  535. s0to1, s0to2, s0to3, s0to4, s0to5, s0to6, s0to7,
  536. s6to7,
  537. d0m1, d2m3, d4m5, d6m7,
  538. d1m0, d3m2, d5m4, d7m6,
  539. d0m1to2m3, d0m1to4m5, d0m1to6m7,
  540. d1m0to3m2, d1m0to5m4, d1m0to7m6,
  541. };
  542. #define MAX1363_8X_CHANS(bits) { \
  543. MAX1363_CHAN_U(0, _s0, 0, bits, NULL, 0), \
  544. MAX1363_CHAN_U(1, _s1, 1, bits, NULL, 0), \
  545. MAX1363_CHAN_U(2, _s2, 2, bits, NULL, 0), \
  546. MAX1363_CHAN_U(3, _s3, 3, bits, NULL, 0), \
  547. MAX1363_CHAN_U(4, _s4, 4, bits, NULL, 0), \
  548. MAX1363_CHAN_U(5, _s5, 5, bits, NULL, 0), \
  549. MAX1363_CHAN_U(6, _s6, 6, bits, NULL, 0), \
  550. MAX1363_CHAN_U(7, _s7, 7, bits, NULL, 0), \
  551. MAX1363_CHAN_B(0, 1, d0m1, 8, bits, NULL, 0), \
  552. MAX1363_CHAN_B(2, 3, d2m3, 9, bits, NULL, 0), \
  553. MAX1363_CHAN_B(4, 5, d4m5, 10, bits, NULL, 0), \
  554. MAX1363_CHAN_B(6, 7, d6m7, 11, bits, NULL, 0), \
  555. MAX1363_CHAN_B(1, 0, d1m0, 12, bits, NULL, 0), \
  556. MAX1363_CHAN_B(3, 2, d3m2, 13, bits, NULL, 0), \
  557. MAX1363_CHAN_B(5, 4, d5m4, 14, bits, NULL, 0), \
  558. MAX1363_CHAN_B(7, 6, d7m6, 15, bits, NULL, 0), \
  559. IIO_CHAN_SOFT_TIMESTAMP(16) \
  560. }
  561. static const struct iio_chan_spec max11602_channels[] = MAX1363_8X_CHANS(8);
  562. static const struct iio_chan_spec max11608_channels[] = MAX1363_8X_CHANS(10);
  563. static const struct iio_chan_spec max11614_channels[] = MAX1363_8X_CHANS(12);
  564. static const enum max1363_modes max11644_mode_list[] = {
  565. _s0, _s1, s0to1, d0m1, d1m0,
  566. };
  567. #define MAX1363_2X_CHANS(bits) { \
  568. MAX1363_CHAN_U(0, _s0, 0, bits, NULL, 0), \
  569. MAX1363_CHAN_U(1, _s1, 1, bits, NULL, 0), \
  570. MAX1363_CHAN_B(0, 1, d0m1, 2, bits, NULL, 0), \
  571. MAX1363_CHAN_B(1, 0, d1m0, 3, bits, NULL, 0), \
  572. IIO_CHAN_SOFT_TIMESTAMP(4) \
  573. }
  574. static const struct iio_chan_spec max11646_channels[] = MAX1363_2X_CHANS(10);
  575. static const struct iio_chan_spec max11644_channels[] = MAX1363_2X_CHANS(12);
  576. enum { max1361,
  577. max1362,
  578. max1363,
  579. max1364,
  580. max1036,
  581. max1037,
  582. max1038,
  583. max1039,
  584. max1136,
  585. max1137,
  586. max1138,
  587. max1139,
  588. max1236,
  589. max1237,
  590. max1238,
  591. max1239,
  592. max11600,
  593. max11601,
  594. max11602,
  595. max11603,
  596. max11604,
  597. max11605,
  598. max11606,
  599. max11607,
  600. max11608,
  601. max11609,
  602. max11610,
  603. max11611,
  604. max11612,
  605. max11613,
  606. max11614,
  607. max11615,
  608. max11616,
  609. max11617,
  610. max11644,
  611. max11645,
  612. max11646,
  613. max11647
  614. };
  615. static const int max1363_monitor_speeds[] = { 133000, 665000, 33300, 16600,
  616. 8300, 4200, 2000, 1000 };
  617. static ssize_t max1363_monitor_show_freq(struct device *dev,
  618. struct device_attribute *attr,
  619. char *buf)
  620. {
  621. struct max1363_state *st = iio_priv(dev_to_iio_dev(dev));
  622. return sprintf(buf, "%d\n", max1363_monitor_speeds[st->monitor_speed]);
  623. }
  624. static ssize_t max1363_monitor_store_freq(struct device *dev,
  625. struct device_attribute *attr,
  626. const char *buf,
  627. size_t len)
  628. {
  629. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  630. struct max1363_state *st = iio_priv(indio_dev);
  631. int i, ret;
  632. unsigned long val;
  633. bool found = false;
  634. ret = kstrtoul(buf, 10, &val);
  635. if (ret)
  636. return -EINVAL;
  637. for (i = 0; i < ARRAY_SIZE(max1363_monitor_speeds); i++)
  638. if (val == max1363_monitor_speeds[i]) {
  639. found = true;
  640. break;
  641. }
  642. if (!found)
  643. return -EINVAL;
  644. mutex_lock(&indio_dev->mlock);
  645. st->monitor_speed = i;
  646. mutex_unlock(&indio_dev->mlock);
  647. return 0;
  648. }
  649. static IIO_DEV_ATTR_SAMP_FREQ(S_IRUGO | S_IWUSR,
  650. max1363_monitor_show_freq,
  651. max1363_monitor_store_freq);
  652. static IIO_CONST_ATTR(sampling_frequency_available,
  653. "133000 665000 33300 16600 8300 4200 2000 1000");
  654. static int max1363_read_thresh(struct iio_dev *indio_dev,
  655. const struct iio_chan_spec *chan, enum iio_event_type type,
  656. enum iio_event_direction dir, enum iio_event_info info, int *val,
  657. int *val2)
  658. {
  659. struct max1363_state *st = iio_priv(indio_dev);
  660. if (dir == IIO_EV_DIR_FALLING)
  661. *val = st->thresh_low[chan->channel];
  662. else
  663. *val = st->thresh_high[chan->channel];
  664. return IIO_VAL_INT;
  665. }
  666. static int max1363_write_thresh(struct iio_dev *indio_dev,
  667. const struct iio_chan_spec *chan, enum iio_event_type type,
  668. enum iio_event_direction dir, enum iio_event_info info, int val,
  669. int val2)
  670. {
  671. struct max1363_state *st = iio_priv(indio_dev);
  672. /* make it handle signed correctly as well */
  673. switch (st->chip_info->bits) {
  674. case 10:
  675. if (val > 0x3FF)
  676. return -EINVAL;
  677. break;
  678. case 12:
  679. if (val > 0xFFF)
  680. return -EINVAL;
  681. break;
  682. }
  683. switch (dir) {
  684. case IIO_EV_DIR_FALLING:
  685. st->thresh_low[chan->channel] = val;
  686. break;
  687. case IIO_EV_DIR_RISING:
  688. st->thresh_high[chan->channel] = val;
  689. break;
  690. default:
  691. return -EINVAL;
  692. }
  693. return 0;
  694. }
  695. static const u64 max1363_event_codes[] = {
  696. IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 0,
  697. IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING),
  698. IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 1,
  699. IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING),
  700. IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 2,
  701. IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING),
  702. IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 3,
  703. IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING),
  704. IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 0,
  705. IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING),
  706. IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 1,
  707. IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING),
  708. IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 2,
  709. IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING),
  710. IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 3,
  711. IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING),
  712. };
  713. static irqreturn_t max1363_event_handler(int irq, void *private)
  714. {
  715. struct iio_dev *indio_dev = private;
  716. struct max1363_state *st = iio_priv(indio_dev);
  717. s64 timestamp = iio_get_time_ns();
  718. unsigned long mask, loc;
  719. u8 rx;
  720. u8 tx[2] = { st->setupbyte,
  721. MAX1363_MON_INT_ENABLE | (st->monitor_speed << 1) | 0xF0 };
  722. st->recv(st->client, &rx, 1);
  723. mask = rx;
  724. for_each_set_bit(loc, &mask, 8)
  725. iio_push_event(indio_dev, max1363_event_codes[loc], timestamp);
  726. st->send(st->client, tx, 2);
  727. return IRQ_HANDLED;
  728. }
  729. static int max1363_read_event_config(struct iio_dev *indio_dev,
  730. const struct iio_chan_spec *chan, enum iio_event_type type,
  731. enum iio_event_direction dir)
  732. {
  733. struct max1363_state *st = iio_priv(indio_dev);
  734. int val;
  735. int number = chan->channel;
  736. mutex_lock(&indio_dev->mlock);
  737. if (dir == IIO_EV_DIR_FALLING)
  738. val = (1 << number) & st->mask_low;
  739. else
  740. val = (1 << number) & st->mask_high;
  741. mutex_unlock(&indio_dev->mlock);
  742. return val;
  743. }
  744. static int max1363_monitor_mode_update(struct max1363_state *st, int enabled)
  745. {
  746. u8 *tx_buf;
  747. int ret, i = 3, j;
  748. unsigned long numelements;
  749. int len;
  750. const long *modemask;
  751. if (!enabled) {
  752. /* transition to buffered capture is not currently supported */
  753. st->setupbyte &= ~MAX1363_SETUP_MONITOR_SETUP;
  754. st->configbyte &= ~MAX1363_SCAN_MASK;
  755. st->monitor_on = false;
  756. return max1363_write_basic_config(st);
  757. }
  758. /* Ensure we are in the relevant mode */
  759. st->setupbyte |= MAX1363_SETUP_MONITOR_SETUP;
  760. st->configbyte &= ~(MAX1363_CHANNEL_SEL_MASK
  761. | MAX1363_SCAN_MASK
  762. | MAX1363_SE_DE_MASK);
  763. st->configbyte |= MAX1363_CONFIG_SCAN_MONITOR_MODE;
  764. if ((st->mask_low | st->mask_high) & 0x0F) {
  765. st->configbyte |= max1363_mode_table[s0to3].conf;
  766. modemask = max1363_mode_table[s0to3].modemask;
  767. } else if ((st->mask_low | st->mask_high) & 0x30) {
  768. st->configbyte |= max1363_mode_table[d0m1to2m3].conf;
  769. modemask = max1363_mode_table[d0m1to2m3].modemask;
  770. } else {
  771. st->configbyte |= max1363_mode_table[d1m0to3m2].conf;
  772. modemask = max1363_mode_table[d1m0to3m2].modemask;
  773. }
  774. numelements = bitmap_weight(modemask, MAX1363_MAX_CHANNELS);
  775. len = 3 * numelements + 3;
  776. tx_buf = kmalloc(len, GFP_KERNEL);
  777. if (!tx_buf) {
  778. ret = -ENOMEM;
  779. goto error_ret;
  780. }
  781. tx_buf[0] = st->configbyte;
  782. tx_buf[1] = st->setupbyte;
  783. tx_buf[2] = (st->monitor_speed << 1);
  784. /*
  785. * So we need to do yet another bit of nefarious scan mode
  786. * setup to match what we need.
  787. */
  788. for (j = 0; j < 8; j++)
  789. if (test_bit(j, modemask)) {
  790. /* Establish the mode is in the scan */
  791. if (st->mask_low & (1 << j)) {
  792. tx_buf[i] = (st->thresh_low[j] >> 4) & 0xFF;
  793. tx_buf[i + 1] = (st->thresh_low[j] << 4) & 0xF0;
  794. } else if (j < 4) {
  795. tx_buf[i] = 0;
  796. tx_buf[i + 1] = 0;
  797. } else {
  798. tx_buf[i] = 0x80;
  799. tx_buf[i + 1] = 0;
  800. }
  801. if (st->mask_high & (1 << j)) {
  802. tx_buf[i + 1] |=
  803. (st->thresh_high[j] >> 8) & 0x0F;
  804. tx_buf[i + 2] = st->thresh_high[j] & 0xFF;
  805. } else if (j < 4) {
  806. tx_buf[i + 1] |= 0x0F;
  807. tx_buf[i + 2] = 0xFF;
  808. } else {
  809. tx_buf[i + 1] |= 0x07;
  810. tx_buf[i + 2] = 0xFF;
  811. }
  812. i += 3;
  813. }
  814. ret = st->send(st->client, tx_buf, len);
  815. if (ret < 0)
  816. goto error_ret;
  817. if (ret != len) {
  818. ret = -EIO;
  819. goto error_ret;
  820. }
  821. /*
  822. * Now that we hopefully have sensible thresholds in place it is
  823. * time to turn the interrupts on.
  824. * It is unclear from the data sheet if this should be necessary
  825. * (i.e. whether monitor mode setup is atomic) but it appears to
  826. * be in practice.
  827. */
  828. tx_buf[0] = st->setupbyte;
  829. tx_buf[1] = MAX1363_MON_INT_ENABLE | (st->monitor_speed << 1) | 0xF0;
  830. ret = st->send(st->client, tx_buf, 2);
  831. if (ret < 0)
  832. goto error_ret;
  833. if (ret != 2) {
  834. ret = -EIO;
  835. goto error_ret;
  836. }
  837. ret = 0;
  838. st->monitor_on = true;
  839. error_ret:
  840. kfree(tx_buf);
  841. return ret;
  842. }
  843. /*
  844. * To keep this manageable we always use one of 3 scan modes.
  845. * Scan 0...3, 0-1,2-3 and 1-0,3-2
  846. */
  847. static inline int __max1363_check_event_mask(int thismask, int checkmask)
  848. {
  849. int ret = 0;
  850. /* Is it unipolar */
  851. if (thismask < 4) {
  852. if (checkmask & ~0x0F) {
  853. ret = -EBUSY;
  854. goto error_ret;
  855. }
  856. } else if (thismask < 6) {
  857. if (checkmask & ~0x30) {
  858. ret = -EBUSY;
  859. goto error_ret;
  860. }
  861. } else if (checkmask & ~0xC0)
  862. ret = -EBUSY;
  863. error_ret:
  864. return ret;
  865. }
  866. static int max1363_write_event_config(struct iio_dev *indio_dev,
  867. const struct iio_chan_spec *chan, enum iio_event_type type,
  868. enum iio_event_direction dir, int state)
  869. {
  870. int ret = 0;
  871. struct max1363_state *st = iio_priv(indio_dev);
  872. u16 unifiedmask;
  873. int number = chan->channel;
  874. mutex_lock(&indio_dev->mlock);
  875. unifiedmask = st->mask_low | st->mask_high;
  876. if (dir == IIO_EV_DIR_FALLING) {
  877. if (state == 0)
  878. st->mask_low &= ~(1 << number);
  879. else {
  880. ret = __max1363_check_event_mask((1 << number),
  881. unifiedmask);
  882. if (ret)
  883. goto error_ret;
  884. st->mask_low |= (1 << number);
  885. }
  886. } else {
  887. if (state == 0)
  888. st->mask_high &= ~(1 << number);
  889. else {
  890. ret = __max1363_check_event_mask((1 << number),
  891. unifiedmask);
  892. if (ret)
  893. goto error_ret;
  894. st->mask_high |= (1 << number);
  895. }
  896. }
  897. max1363_monitor_mode_update(st, !!(st->mask_high | st->mask_low));
  898. error_ret:
  899. mutex_unlock(&indio_dev->mlock);
  900. return ret;
  901. }
  902. /*
  903. * As with scan_elements, only certain sets of these can
  904. * be combined.
  905. */
  906. static struct attribute *max1363_event_attributes[] = {
  907. &iio_dev_attr_sampling_frequency.dev_attr.attr,
  908. &iio_const_attr_sampling_frequency_available.dev_attr.attr,
  909. NULL,
  910. };
  911. static struct attribute_group max1363_event_attribute_group = {
  912. .attrs = max1363_event_attributes,
  913. .name = "events",
  914. };
  915. static int max1363_update_scan_mode(struct iio_dev *indio_dev,
  916. const unsigned long *scan_mask)
  917. {
  918. struct max1363_state *st = iio_priv(indio_dev);
  919. /*
  920. * Need to figure out the current mode based upon the requested
  921. * scan mask in iio_dev
  922. */
  923. st->current_mode = max1363_match_mode(scan_mask, st->chip_info);
  924. if (!st->current_mode)
  925. return -EINVAL;
  926. max1363_set_scan_mode(st);
  927. return 0;
  928. }
  929. static const struct iio_info max1238_info = {
  930. .read_raw = &max1363_read_raw,
  931. .driver_module = THIS_MODULE,
  932. .update_scan_mode = &max1363_update_scan_mode,
  933. };
  934. static const struct iio_info max1363_info = {
  935. .read_event_value = &max1363_read_thresh,
  936. .write_event_value = &max1363_write_thresh,
  937. .read_event_config = &max1363_read_event_config,
  938. .write_event_config = &max1363_write_event_config,
  939. .read_raw = &max1363_read_raw,
  940. .update_scan_mode = &max1363_update_scan_mode,
  941. .driver_module = THIS_MODULE,
  942. .event_attrs = &max1363_event_attribute_group,
  943. };
  944. /* max1363 and max1368 tested - rest from data sheet */
  945. static const struct max1363_chip_info max1363_chip_info_tbl[] = {
  946. [max1361] = {
  947. .bits = 10,
  948. .int_vref_mv = 2048,
  949. .mode_list = max1363_mode_list,
  950. .num_modes = ARRAY_SIZE(max1363_mode_list),
  951. .default_mode = s0to3,
  952. .channels = max1361_channels,
  953. .num_channels = ARRAY_SIZE(max1361_channels),
  954. .info = &max1363_info,
  955. },
  956. [max1362] = {
  957. .bits = 10,
  958. .int_vref_mv = 4096,
  959. .mode_list = max1363_mode_list,
  960. .num_modes = ARRAY_SIZE(max1363_mode_list),
  961. .default_mode = s0to3,
  962. .channels = max1361_channels,
  963. .num_channels = ARRAY_SIZE(max1361_channels),
  964. .info = &max1363_info,
  965. },
  966. [max1363] = {
  967. .bits = 12,
  968. .int_vref_mv = 2048,
  969. .mode_list = max1363_mode_list,
  970. .num_modes = ARRAY_SIZE(max1363_mode_list),
  971. .default_mode = s0to3,
  972. .channels = max1363_channels,
  973. .num_channels = ARRAY_SIZE(max1363_channels),
  974. .info = &max1363_info,
  975. },
  976. [max1364] = {
  977. .bits = 12,
  978. .int_vref_mv = 4096,
  979. .mode_list = max1363_mode_list,
  980. .num_modes = ARRAY_SIZE(max1363_mode_list),
  981. .default_mode = s0to3,
  982. .channels = max1363_channels,
  983. .num_channels = ARRAY_SIZE(max1363_channels),
  984. .info = &max1363_info,
  985. },
  986. [max1036] = {
  987. .bits = 8,
  988. .int_vref_mv = 4096,
  989. .mode_list = max1236_mode_list,
  990. .num_modes = ARRAY_SIZE(max1236_mode_list),
  991. .default_mode = s0to3,
  992. .info = &max1238_info,
  993. .channels = max1036_channels,
  994. .num_channels = ARRAY_SIZE(max1036_channels),
  995. },
  996. [max1037] = {
  997. .bits = 8,
  998. .int_vref_mv = 2048,
  999. .mode_list = max1236_mode_list,
  1000. .num_modes = ARRAY_SIZE(max1236_mode_list),
  1001. .default_mode = s0to3,
  1002. .info = &max1238_info,
  1003. .channels = max1036_channels,
  1004. .num_channels = ARRAY_SIZE(max1036_channels),
  1005. },
  1006. [max1038] = {
  1007. .bits = 8,
  1008. .int_vref_mv = 4096,
  1009. .mode_list = max1238_mode_list,
  1010. .num_modes = ARRAY_SIZE(max1238_mode_list),
  1011. .default_mode = s0to11,
  1012. .info = &max1238_info,
  1013. .channels = max1038_channels,
  1014. .num_channels = ARRAY_SIZE(max1038_channels),
  1015. },
  1016. [max1039] = {
  1017. .bits = 8,
  1018. .int_vref_mv = 2048,
  1019. .mode_list = max1238_mode_list,
  1020. .num_modes = ARRAY_SIZE(max1238_mode_list),
  1021. .default_mode = s0to11,
  1022. .info = &max1238_info,
  1023. .channels = max1038_channels,
  1024. .num_channels = ARRAY_SIZE(max1038_channels),
  1025. },
  1026. [max1136] = {
  1027. .bits = 10,
  1028. .int_vref_mv = 4096,
  1029. .mode_list = max1236_mode_list,
  1030. .num_modes = ARRAY_SIZE(max1236_mode_list),
  1031. .default_mode = s0to3,
  1032. .info = &max1238_info,
  1033. .channels = max1136_channels,
  1034. .num_channels = ARRAY_SIZE(max1136_channels),
  1035. },
  1036. [max1137] = {
  1037. .bits = 10,
  1038. .int_vref_mv = 2048,
  1039. .mode_list = max1236_mode_list,
  1040. .num_modes = ARRAY_SIZE(max1236_mode_list),
  1041. .default_mode = s0to3,
  1042. .info = &max1238_info,
  1043. .channels = max1136_channels,
  1044. .num_channels = ARRAY_SIZE(max1136_channels),
  1045. },
  1046. [max1138] = {
  1047. .bits = 10,
  1048. .int_vref_mv = 4096,
  1049. .mode_list = max1238_mode_list,
  1050. .num_modes = ARRAY_SIZE(max1238_mode_list),
  1051. .default_mode = s0to11,
  1052. .info = &max1238_info,
  1053. .channels = max1138_channels,
  1054. .num_channels = ARRAY_SIZE(max1138_channels),
  1055. },
  1056. [max1139] = {
  1057. .bits = 10,
  1058. .int_vref_mv = 2048,
  1059. .mode_list = max1238_mode_list,
  1060. .num_modes = ARRAY_SIZE(max1238_mode_list),
  1061. .default_mode = s0to11,
  1062. .info = &max1238_info,
  1063. .channels = max1138_channels,
  1064. .num_channels = ARRAY_SIZE(max1138_channels),
  1065. },
  1066. [max1236] = {
  1067. .bits = 12,
  1068. .int_vref_mv = 4096,
  1069. .mode_list = max1236_mode_list,
  1070. .num_modes = ARRAY_SIZE(max1236_mode_list),
  1071. .default_mode = s0to3,
  1072. .info = &max1238_info,
  1073. .channels = max1236_channels,
  1074. .num_channels = ARRAY_SIZE(max1236_channels),
  1075. },
  1076. [max1237] = {
  1077. .bits = 12,
  1078. .int_vref_mv = 2048,
  1079. .mode_list = max1236_mode_list,
  1080. .num_modes = ARRAY_SIZE(max1236_mode_list),
  1081. .default_mode = s0to3,
  1082. .info = &max1238_info,
  1083. .channels = max1236_channels,
  1084. .num_channels = ARRAY_SIZE(max1236_channels),
  1085. },
  1086. [max1238] = {
  1087. .bits = 12,
  1088. .int_vref_mv = 4096,
  1089. .mode_list = max1238_mode_list,
  1090. .num_modes = ARRAY_SIZE(max1238_mode_list),
  1091. .default_mode = s0to11,
  1092. .info = &max1238_info,
  1093. .channels = max1238_channels,
  1094. .num_channels = ARRAY_SIZE(max1238_channels),
  1095. },
  1096. [max1239] = {
  1097. .bits = 12,
  1098. .int_vref_mv = 2048,
  1099. .mode_list = max1238_mode_list,
  1100. .num_modes = ARRAY_SIZE(max1238_mode_list),
  1101. .default_mode = s0to11,
  1102. .info = &max1238_info,
  1103. .channels = max1238_channels,
  1104. .num_channels = ARRAY_SIZE(max1238_channels),
  1105. },
  1106. [max11600] = {
  1107. .bits = 8,
  1108. .int_vref_mv = 4096,
  1109. .mode_list = max11607_mode_list,
  1110. .num_modes = ARRAY_SIZE(max11607_mode_list),
  1111. .default_mode = s0to3,
  1112. .info = &max1238_info,
  1113. .channels = max1036_channels,
  1114. .num_channels = ARRAY_SIZE(max1036_channels),
  1115. },
  1116. [max11601] = {
  1117. .bits = 8,
  1118. .int_vref_mv = 2048,
  1119. .mode_list = max11607_mode_list,
  1120. .num_modes = ARRAY_SIZE(max11607_mode_list),
  1121. .default_mode = s0to3,
  1122. .info = &max1238_info,
  1123. .channels = max1036_channels,
  1124. .num_channels = ARRAY_SIZE(max1036_channels),
  1125. },
  1126. [max11602] = {
  1127. .bits = 8,
  1128. .int_vref_mv = 4096,
  1129. .mode_list = max11608_mode_list,
  1130. .num_modes = ARRAY_SIZE(max11608_mode_list),
  1131. .default_mode = s0to7,
  1132. .info = &max1238_info,
  1133. .channels = max11602_channels,
  1134. .num_channels = ARRAY_SIZE(max11602_channels),
  1135. },
  1136. [max11603] = {
  1137. .bits = 8,
  1138. .int_vref_mv = 2048,
  1139. .mode_list = max11608_mode_list,
  1140. .num_modes = ARRAY_SIZE(max11608_mode_list),
  1141. .default_mode = s0to7,
  1142. .info = &max1238_info,
  1143. .channels = max11602_channels,
  1144. .num_channels = ARRAY_SIZE(max11602_channels),
  1145. },
  1146. [max11604] = {
  1147. .bits = 8,
  1148. .int_vref_mv = 4096,
  1149. .mode_list = max1238_mode_list,
  1150. .num_modes = ARRAY_SIZE(max1238_mode_list),
  1151. .default_mode = s0to11,
  1152. .info = &max1238_info,
  1153. .channels = max1038_channels,
  1154. .num_channels = ARRAY_SIZE(max1038_channels),
  1155. },
  1156. [max11605] = {
  1157. .bits = 8,
  1158. .int_vref_mv = 2048,
  1159. .mode_list = max1238_mode_list,
  1160. .num_modes = ARRAY_SIZE(max1238_mode_list),
  1161. .default_mode = s0to11,
  1162. .info = &max1238_info,
  1163. .channels = max1038_channels,
  1164. .num_channels = ARRAY_SIZE(max1038_channels),
  1165. },
  1166. [max11606] = {
  1167. .bits = 10,
  1168. .int_vref_mv = 4096,
  1169. .mode_list = max11607_mode_list,
  1170. .num_modes = ARRAY_SIZE(max11607_mode_list),
  1171. .default_mode = s0to3,
  1172. .info = &max1238_info,
  1173. .channels = max1136_channels,
  1174. .num_channels = ARRAY_SIZE(max1136_channels),
  1175. },
  1176. [max11607] = {
  1177. .bits = 10,
  1178. .int_vref_mv = 2048,
  1179. .mode_list = max11607_mode_list,
  1180. .num_modes = ARRAY_SIZE(max11607_mode_list),
  1181. .default_mode = s0to3,
  1182. .info = &max1238_info,
  1183. .channels = max1136_channels,
  1184. .num_channels = ARRAY_SIZE(max1136_channels),
  1185. },
  1186. [max11608] = {
  1187. .bits = 10,
  1188. .int_vref_mv = 4096,
  1189. .mode_list = max11608_mode_list,
  1190. .num_modes = ARRAY_SIZE(max11608_mode_list),
  1191. .default_mode = s0to7,
  1192. .info = &max1238_info,
  1193. .channels = max11608_channels,
  1194. .num_channels = ARRAY_SIZE(max11608_channels),
  1195. },
  1196. [max11609] = {
  1197. .bits = 10,
  1198. .int_vref_mv = 2048,
  1199. .mode_list = max11608_mode_list,
  1200. .num_modes = ARRAY_SIZE(max11608_mode_list),
  1201. .default_mode = s0to7,
  1202. .info = &max1238_info,
  1203. .channels = max11608_channels,
  1204. .num_channels = ARRAY_SIZE(max11608_channels),
  1205. },
  1206. [max11610] = {
  1207. .bits = 10,
  1208. .int_vref_mv = 4096,
  1209. .mode_list = max1238_mode_list,
  1210. .num_modes = ARRAY_SIZE(max1238_mode_list),
  1211. .default_mode = s0to11,
  1212. .info = &max1238_info,
  1213. .channels = max1138_channels,
  1214. .num_channels = ARRAY_SIZE(max1138_channels),
  1215. },
  1216. [max11611] = {
  1217. .bits = 10,
  1218. .int_vref_mv = 2048,
  1219. .mode_list = max1238_mode_list,
  1220. .num_modes = ARRAY_SIZE(max1238_mode_list),
  1221. .default_mode = s0to11,
  1222. .info = &max1238_info,
  1223. .channels = max1138_channels,
  1224. .num_channels = ARRAY_SIZE(max1138_channels),
  1225. },
  1226. [max11612] = {
  1227. .bits = 12,
  1228. .int_vref_mv = 4096,
  1229. .mode_list = max11607_mode_list,
  1230. .num_modes = ARRAY_SIZE(max11607_mode_list),
  1231. .default_mode = s0to3,
  1232. .info = &max1238_info,
  1233. .channels = max1363_channels,
  1234. .num_channels = ARRAY_SIZE(max1363_channels),
  1235. },
  1236. [max11613] = {
  1237. .bits = 12,
  1238. .int_vref_mv = 2048,
  1239. .mode_list = max11607_mode_list,
  1240. .num_modes = ARRAY_SIZE(max11607_mode_list),
  1241. .default_mode = s0to3,
  1242. .info = &max1238_info,
  1243. .channels = max1363_channels,
  1244. .num_channels = ARRAY_SIZE(max1363_channels),
  1245. },
  1246. [max11614] = {
  1247. .bits = 12,
  1248. .int_vref_mv = 4096,
  1249. .mode_list = max11608_mode_list,
  1250. .num_modes = ARRAY_SIZE(max11608_mode_list),
  1251. .default_mode = s0to7,
  1252. .info = &max1238_info,
  1253. .channels = max11614_channels,
  1254. .num_channels = ARRAY_SIZE(max11614_channels),
  1255. },
  1256. [max11615] = {
  1257. .bits = 12,
  1258. .int_vref_mv = 2048,
  1259. .mode_list = max11608_mode_list,
  1260. .num_modes = ARRAY_SIZE(max11608_mode_list),
  1261. .default_mode = s0to7,
  1262. .info = &max1238_info,
  1263. .channels = max11614_channels,
  1264. .num_channels = ARRAY_SIZE(max11614_channels),
  1265. },
  1266. [max11616] = {
  1267. .bits = 12,
  1268. .int_vref_mv = 4096,
  1269. .mode_list = max1238_mode_list,
  1270. .num_modes = ARRAY_SIZE(max1238_mode_list),
  1271. .default_mode = s0to11,
  1272. .info = &max1238_info,
  1273. .channels = max1238_channels,
  1274. .num_channels = ARRAY_SIZE(max1238_channels),
  1275. },
  1276. [max11617] = {
  1277. .bits = 12,
  1278. .int_vref_mv = 2048,
  1279. .mode_list = max1238_mode_list,
  1280. .num_modes = ARRAY_SIZE(max1238_mode_list),
  1281. .default_mode = s0to11,
  1282. .info = &max1238_info,
  1283. .channels = max1238_channels,
  1284. .num_channels = ARRAY_SIZE(max1238_channels),
  1285. },
  1286. [max11644] = {
  1287. .bits = 12,
  1288. .int_vref_mv = 2048,
  1289. .mode_list = max11644_mode_list,
  1290. .num_modes = ARRAY_SIZE(max11644_mode_list),
  1291. .default_mode = s0to1,
  1292. .info = &max1238_info,
  1293. .channels = max11644_channels,
  1294. .num_channels = ARRAY_SIZE(max11644_channels),
  1295. },
  1296. [max11645] = {
  1297. .bits = 12,
  1298. .int_vref_mv = 4096,
  1299. .mode_list = max11644_mode_list,
  1300. .num_modes = ARRAY_SIZE(max11644_mode_list),
  1301. .default_mode = s0to1,
  1302. .info = &max1238_info,
  1303. .channels = max11644_channels,
  1304. .num_channels = ARRAY_SIZE(max11644_channels),
  1305. },
  1306. [max11646] = {
  1307. .bits = 10,
  1308. .int_vref_mv = 2048,
  1309. .mode_list = max11644_mode_list,
  1310. .num_modes = ARRAY_SIZE(max11644_mode_list),
  1311. .default_mode = s0to1,
  1312. .info = &max1238_info,
  1313. .channels = max11646_channels,
  1314. .num_channels = ARRAY_SIZE(max11646_channels),
  1315. },
  1316. [max11647] = {
  1317. .bits = 10,
  1318. .int_vref_mv = 4096,
  1319. .mode_list = max11644_mode_list,
  1320. .num_modes = ARRAY_SIZE(max11644_mode_list),
  1321. .default_mode = s0to1,
  1322. .info = &max1238_info,
  1323. .channels = max11646_channels,
  1324. .num_channels = ARRAY_SIZE(max11646_channels),
  1325. },
  1326. };
  1327. static int max1363_initial_setup(struct max1363_state *st)
  1328. {
  1329. st->setupbyte = MAX1363_SETUP_INT_CLOCK
  1330. | MAX1363_SETUP_UNIPOLAR
  1331. | MAX1363_SETUP_NORESET;
  1332. if (st->vref)
  1333. st->setupbyte |= MAX1363_SETUP_AIN3_IS_REF_EXT_TO_REF;
  1334. else
  1335. st->setupbyte |= MAX1363_SETUP_POWER_UP_INT_REF
  1336. | MAX1363_SETUP_AIN3_IS_AIN3_REF_IS_INT;
  1337. /* Set scan mode writes the config anyway so wait until then */
  1338. st->setupbyte = MAX1363_SETUP_BYTE(st->setupbyte);
  1339. st->current_mode = &max1363_mode_table[st->chip_info->default_mode];
  1340. st->configbyte = MAX1363_CONFIG_BYTE(st->configbyte);
  1341. return max1363_set_scan_mode(st);
  1342. }
  1343. static int max1363_alloc_scan_masks(struct iio_dev *indio_dev)
  1344. {
  1345. struct max1363_state *st = iio_priv(indio_dev);
  1346. unsigned long *masks;
  1347. int i;
  1348. masks = devm_kzalloc(&indio_dev->dev,
  1349. BITS_TO_LONGS(MAX1363_MAX_CHANNELS) * sizeof(long) *
  1350. (st->chip_info->num_modes + 1), GFP_KERNEL);
  1351. if (!masks)
  1352. return -ENOMEM;
  1353. for (i = 0; i < st->chip_info->num_modes; i++)
  1354. bitmap_copy(masks + BITS_TO_LONGS(MAX1363_MAX_CHANNELS)*i,
  1355. max1363_mode_table[st->chip_info->mode_list[i]]
  1356. .modemask, MAX1363_MAX_CHANNELS);
  1357. indio_dev->available_scan_masks = masks;
  1358. return 0;
  1359. }
  1360. static irqreturn_t max1363_trigger_handler(int irq, void *p)
  1361. {
  1362. struct iio_poll_func *pf = p;
  1363. struct iio_dev *indio_dev = pf->indio_dev;
  1364. struct max1363_state *st = iio_priv(indio_dev);
  1365. __u8 *rxbuf;
  1366. int b_sent;
  1367. size_t d_size;
  1368. unsigned long numvals = bitmap_weight(st->current_mode->modemask,
  1369. MAX1363_MAX_CHANNELS);
  1370. /* Ensure the timestamp is 8 byte aligned */
  1371. if (st->chip_info->bits != 8)
  1372. d_size = numvals*2;
  1373. else
  1374. d_size = numvals;
  1375. if (indio_dev->scan_timestamp) {
  1376. d_size += sizeof(s64);
  1377. if (d_size % sizeof(s64))
  1378. d_size += sizeof(s64) - (d_size % sizeof(s64));
  1379. }
  1380. /* Monitor mode prevents reading. Whilst not currently implemented
  1381. * might as well have this test in here in the meantime as it does
  1382. * no harm.
  1383. */
  1384. if (numvals == 0)
  1385. goto done;
  1386. rxbuf = kmalloc(d_size, GFP_KERNEL);
  1387. if (rxbuf == NULL)
  1388. goto done;
  1389. if (st->chip_info->bits != 8)
  1390. b_sent = st->recv(st->client, rxbuf, numvals * 2);
  1391. else
  1392. b_sent = st->recv(st->client, rxbuf, numvals);
  1393. if (b_sent < 0)
  1394. goto done_free;
  1395. iio_push_to_buffers_with_timestamp(indio_dev, rxbuf, iio_get_time_ns());
  1396. done_free:
  1397. kfree(rxbuf);
  1398. done:
  1399. iio_trigger_notify_done(indio_dev->trig);
  1400. return IRQ_HANDLED;
  1401. }
  1402. static int max1363_probe(struct i2c_client *client,
  1403. const struct i2c_device_id *id)
  1404. {
  1405. int ret;
  1406. struct max1363_state *st;
  1407. struct iio_dev *indio_dev;
  1408. struct regulator *vref;
  1409. indio_dev = devm_iio_device_alloc(&client->dev,
  1410. sizeof(struct max1363_state));
  1411. if (!indio_dev)
  1412. return -ENOMEM;
  1413. indio_dev->dev.of_node = client->dev.of_node;
  1414. ret = iio_map_array_register(indio_dev, client->dev.platform_data);
  1415. if (ret < 0)
  1416. return ret;
  1417. st = iio_priv(indio_dev);
  1418. st->reg = devm_regulator_get(&client->dev, "vcc");
  1419. if (IS_ERR(st->reg)) {
  1420. ret = PTR_ERR(st->reg);
  1421. goto error_unregister_map;
  1422. }
  1423. ret = regulator_enable(st->reg);
  1424. if (ret)
  1425. goto error_unregister_map;
  1426. /* this is only used for device removal purposes */
  1427. i2c_set_clientdata(client, indio_dev);
  1428. st->chip_info = &max1363_chip_info_tbl[id->driver_data];
  1429. st->client = client;
  1430. st->vref_uv = st->chip_info->int_vref_mv * 1000;
  1431. vref = devm_regulator_get_optional(&client->dev, "vref");
  1432. if (!IS_ERR(vref)) {
  1433. int vref_uv;
  1434. ret = regulator_enable(vref);
  1435. if (ret)
  1436. goto error_disable_reg;
  1437. st->vref = vref;
  1438. vref_uv = regulator_get_voltage(vref);
  1439. if (vref_uv <= 0) {
  1440. ret = -EINVAL;
  1441. goto error_disable_reg;
  1442. }
  1443. st->vref_uv = vref_uv;
  1444. }
  1445. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  1446. st->send = i2c_master_send;
  1447. st->recv = i2c_master_recv;
  1448. } else if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE)
  1449. && st->chip_info->bits == 8) {
  1450. st->send = max1363_smbus_send;
  1451. st->recv = max1363_smbus_recv;
  1452. } else {
  1453. ret = -EOPNOTSUPP;
  1454. goto error_disable_reg;
  1455. }
  1456. ret = max1363_alloc_scan_masks(indio_dev);
  1457. if (ret)
  1458. goto error_disable_reg;
  1459. /* Establish that the iio_dev is a child of the i2c device */
  1460. indio_dev->dev.parent = &client->dev;
  1461. indio_dev->name = id->name;
  1462. indio_dev->channels = st->chip_info->channels;
  1463. indio_dev->num_channels = st->chip_info->num_channels;
  1464. indio_dev->info = st->chip_info->info;
  1465. indio_dev->modes = INDIO_DIRECT_MODE;
  1466. ret = max1363_initial_setup(st);
  1467. if (ret < 0)
  1468. goto error_disable_reg;
  1469. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  1470. &max1363_trigger_handler, NULL);
  1471. if (ret)
  1472. goto error_disable_reg;
  1473. if (client->irq) {
  1474. ret = devm_request_threaded_irq(&client->dev, st->client->irq,
  1475. NULL,
  1476. &max1363_event_handler,
  1477. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  1478. "max1363_event",
  1479. indio_dev);
  1480. if (ret)
  1481. goto error_uninit_buffer;
  1482. }
  1483. ret = iio_device_register(indio_dev);
  1484. if (ret < 0)
  1485. goto error_uninit_buffer;
  1486. return 0;
  1487. error_uninit_buffer:
  1488. iio_triggered_buffer_cleanup(indio_dev);
  1489. error_disable_reg:
  1490. if (st->vref)
  1491. regulator_disable(st->vref);
  1492. regulator_disable(st->reg);
  1493. error_unregister_map:
  1494. iio_map_array_unregister(indio_dev);
  1495. return ret;
  1496. }
  1497. static int max1363_remove(struct i2c_client *client)
  1498. {
  1499. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  1500. struct max1363_state *st = iio_priv(indio_dev);
  1501. iio_device_unregister(indio_dev);
  1502. iio_triggered_buffer_cleanup(indio_dev);
  1503. if (st->vref)
  1504. regulator_disable(st->vref);
  1505. regulator_disable(st->reg);
  1506. iio_map_array_unregister(indio_dev);
  1507. return 0;
  1508. }
  1509. static const struct i2c_device_id max1363_id[] = {
  1510. { "max1361", max1361 },
  1511. { "max1362", max1362 },
  1512. { "max1363", max1363 },
  1513. { "max1364", max1364 },
  1514. { "max1036", max1036 },
  1515. { "max1037", max1037 },
  1516. { "max1038", max1038 },
  1517. { "max1039", max1039 },
  1518. { "max1136", max1136 },
  1519. { "max1137", max1137 },
  1520. { "max1138", max1138 },
  1521. { "max1139", max1139 },
  1522. { "max1236", max1236 },
  1523. { "max1237", max1237 },
  1524. { "max1238", max1238 },
  1525. { "max1239", max1239 },
  1526. { "max11600", max11600 },
  1527. { "max11601", max11601 },
  1528. { "max11602", max11602 },
  1529. { "max11603", max11603 },
  1530. { "max11604", max11604 },
  1531. { "max11605", max11605 },
  1532. { "max11606", max11606 },
  1533. { "max11607", max11607 },
  1534. { "max11608", max11608 },
  1535. { "max11609", max11609 },
  1536. { "max11610", max11610 },
  1537. { "max11611", max11611 },
  1538. { "max11612", max11612 },
  1539. { "max11613", max11613 },
  1540. { "max11614", max11614 },
  1541. { "max11615", max11615 },
  1542. { "max11616", max11616 },
  1543. { "max11617", max11617 },
  1544. {}
  1545. };
  1546. MODULE_DEVICE_TABLE(i2c, max1363_id);
  1547. static struct i2c_driver max1363_driver = {
  1548. .driver = {
  1549. .name = "max1363",
  1550. },
  1551. .probe = max1363_probe,
  1552. .remove = max1363_remove,
  1553. .id_table = max1363_id,
  1554. };
  1555. module_i2c_driver(max1363_driver);
  1556. MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
  1557. MODULE_DESCRIPTION("Maxim 1363 ADC");
  1558. MODULE_LICENSE("GPL v2");