aw96103.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * AWINIC aw96103 proximity sensor driver
  4. *
  5. * Author: Wang Shuaijie <wangshuaijie@awinic.com>
  6. *
  7. * Copyright (c) 2024 awinic Technology CO., LTD
  8. */
  9. #include <linux/bits.h>
  10. #include <linux/bitfield.h>
  11. #include <linux/delay.h>
  12. #include <linux/firmware.h>
  13. #include <linux/i2c.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/iio/events.h>
  16. #include <linux/iio/iio.h>
  17. #include <linux/regulator/consumer.h>
  18. #include <linux/regmap.h>
  19. #include <linux/slab.h>
  20. #include <linux/unaligned.h>
  21. #define AW_DATA_PROCESS_FACTOR 1024
  22. #define AW96103_CHIP_ID 0xa961
  23. #define AW96103_BIN_VALID_DATA_OFFSET 64
  24. #define AW96103_BIN_DATA_LEN_OFFSET 16
  25. #define AW96103_BIN_DATA_REG_NUM_SIZE 4
  26. #define AW96103_BIN_CHIP_TYPE_SIZE 8
  27. #define AW96103_BIN_CHIP_TYPE_OFFSET 24
  28. #define AW96103_REG_SCANCTRL0 0x0000
  29. #define AW96103_REG_STAT0 0x0090
  30. #define AW96103_REG_BLFILT_CH0 0x00A8
  31. #define AW96103_REG_BLRSTRNG_CH0 0x00B4
  32. #define AW96103_REG_DIFF_CH0 0x0240
  33. #define AW96103_REG_FWVER2 0x0410
  34. #define AW96103_REG_CMD 0xF008
  35. #define AW96103_REG_IRQSRC 0xF080
  36. #define AW96103_REG_IRQEN 0xF084
  37. #define AW96103_REG_RESET 0xFF0C
  38. #define AW96103_REG_CHIPID 0xFF10
  39. #define AW96103_REG_EEDA0 0x0408
  40. #define AW96103_REG_EEDA1 0x040C
  41. #define AW96103_REG_PROXCTRL_CH0 0x00B0
  42. #define AW96103_REG_PROXTH0_CH0 0x00B8
  43. #define AW96103_PROXTH_CH_STEP 0x3C
  44. #define AW96103_THHYST_MASK GENMASK(13, 12)
  45. #define AW96103_INDEB_MASK GENMASK(11, 10)
  46. #define AW96103_OUTDEB_MASK GENMASK(9, 8)
  47. #define AW96103_INITOVERIRQ_MASK BIT(0)
  48. #define AW96103_BLFILT_CH_STEP 0x3C
  49. #define AW96103_BLRSTRNG_MASK GENMASK(5, 0)
  50. #define AW96103_CHIPID_MASK GENMASK(31, 16)
  51. #define AW96103_BLERRTRIG_MASK BIT(25)
  52. #define AW96103_CHAN_EN_MASK GENMASK(5, 0)
  53. #define AW96103_REG_PROXCTRL_CH(x) \
  54. (AW96103_REG_PROXCTRL_CH0 + (x) * AW96103_PROXTH_CH_STEP)
  55. #define AW96103_REG_PROXTH0_CH(x) \
  56. (AW96103_REG_PROXTH0_CH0 + (x) * AW96103_PROXTH_CH_STEP)
  57. /**
  58. * struct aw_bin - Store the data obtained from parsing the configuration file.
  59. * @chip_type: Frame header information-chip type
  60. * @valid_data_len: Length of valid data obtained after parsing
  61. * @valid_data_addr: The offset address of the valid data obtained
  62. * after parsing relative to info
  63. * @len: The size of the bin file obtained from the firmware
  64. * @data: Store the bin file obtained from the firmware
  65. */
  66. struct aw_bin {
  67. unsigned char chip_type[8];
  68. unsigned int valid_data_len;
  69. unsigned int valid_data_addr;
  70. unsigned int len;
  71. unsigned char data[] __counted_by(len);
  72. };
  73. enum aw96103_sar_vers {
  74. AW96103 = 2,
  75. AW96103A = 6,
  76. AW96103B = 0xa,
  77. };
  78. enum aw96103_operation_mode {
  79. AW96103_ACTIVE_MODE = 1,
  80. AW96103_SLEEP_MODE = 2,
  81. AW96103_DEEPSLEEP_MODE = 3,
  82. AW96103B_DEEPSLEEP_MODE = 4,
  83. };
  84. enum aw96103_sensor_type {
  85. AW96103_VAL,
  86. AW96105_VAL,
  87. };
  88. struct aw_channels_info {
  89. bool used;
  90. unsigned int old_irq_status;
  91. };
  92. struct aw_chip_info {
  93. const char *name;
  94. struct iio_chan_spec const *channels;
  95. int num_channels;
  96. };
  97. struct aw96103 {
  98. unsigned int hostirqen;
  99. struct regmap *regmap;
  100. struct device *dev;
  101. /*
  102. * There is one more logical channel than the actual channels,
  103. * and the extra logical channel is used for temperature detection
  104. * but not for status detection. The specific channel used for
  105. * temperature detection is determined by the register configuration.
  106. */
  107. struct aw_channels_info channels_arr[6];
  108. unsigned int max_channels;
  109. unsigned int chan_en;
  110. };
  111. static const unsigned int aw96103_reg_default[] = {
  112. 0x0000, 0x00003f3f, 0x0004, 0x00000064, 0x0008, 0x0017c11e,
  113. 0x000c, 0x05000000, 0x0010, 0x00093ffd, 0x0014, 0x19240009,
  114. 0x0018, 0xd81c0207, 0x001c, 0xff000000, 0x0020, 0x00241900,
  115. 0x0024, 0x00093ff7, 0x0028, 0x58020009, 0x002c, 0xd81c0207,
  116. 0x0030, 0xff000000, 0x0034, 0x00025800, 0x0038, 0x00093fdf,
  117. 0x003c, 0x7d3b0009, 0x0040, 0xd81c0207, 0x0044, 0xff000000,
  118. 0x0048, 0x003b7d00, 0x004c, 0x00093f7f, 0x0050, 0xe9310009,
  119. 0x0054, 0xd81c0207, 0x0058, 0xff000000, 0x005c, 0x0031e900,
  120. 0x0060, 0x00093dff, 0x0064, 0x1a0c0009, 0x0068, 0xd81c0207,
  121. 0x006c, 0xff000000, 0x0070, 0x000c1a00, 0x0074, 0x80093fff,
  122. 0x0078, 0x043d0009, 0x007c, 0xd81c0207, 0x0080, 0xff000000,
  123. 0x0084, 0x003d0400, 0x00a0, 0xe6400000, 0x00a4, 0x00000000,
  124. 0x00a8, 0x010408d2, 0x00ac, 0x00000000, 0x00b0, 0x00000000,
  125. 0x00b8, 0x00005fff, 0x00bc, 0x00000000, 0x00c0, 0x00000000,
  126. 0x00c4, 0x00000000, 0x00c8, 0x00000000, 0x00cc, 0x00000000,
  127. 0x00d0, 0x00000000, 0x00d4, 0x00000000, 0x00d8, 0x00000000,
  128. 0x00dc, 0xe6447800, 0x00e0, 0x78000000, 0x00e4, 0x010408d2,
  129. 0x00e8, 0x00000000, 0x00ec, 0x00000000, 0x00f4, 0x00005fff,
  130. 0x00f8, 0x00000000, 0x00fc, 0x00000000, 0x0100, 0x00000000,
  131. 0x0104, 0x00000000, 0x0108, 0x00000000, 0x010c, 0x02000000,
  132. 0x0110, 0x00000000, 0x0114, 0x00000000, 0x0118, 0xe6447800,
  133. 0x011c, 0x78000000, 0x0120, 0x010408d2, 0x0124, 0x00000000,
  134. 0x0128, 0x00000000, 0x0130, 0x00005fff, 0x0134, 0x00000000,
  135. 0x0138, 0x00000000, 0x013c, 0x00000000, 0x0140, 0x00000000,
  136. 0x0144, 0x00000000, 0x0148, 0x02000000, 0x014c, 0x00000000,
  137. 0x0150, 0x00000000, 0x0154, 0xe6447800, 0x0158, 0x78000000,
  138. 0x015c, 0x010408d2, 0x0160, 0x00000000, 0x0164, 0x00000000,
  139. 0x016c, 0x00005fff, 0x0170, 0x00000000, 0x0174, 0x00000000,
  140. 0x0178, 0x00000000, 0x017c, 0x00000000, 0x0180, 0x00000000,
  141. 0x0184, 0x02000000, 0x0188, 0x00000000, 0x018c, 0x00000000,
  142. 0x0190, 0xe6447800, 0x0194, 0x78000000, 0x0198, 0x010408d2,
  143. 0x019c, 0x00000000, 0x01a0, 0x00000000, 0x01a8, 0x00005fff,
  144. 0x01ac, 0x00000000, 0x01b0, 0x00000000, 0x01b4, 0x00000000,
  145. 0x01b8, 0x00000000, 0x01bc, 0x00000000, 0x01c0, 0x02000000,
  146. 0x01c4, 0x00000000, 0x01c8, 0x00000000, 0x01cc, 0xe6407800,
  147. 0x01d0, 0x78000000, 0x01d4, 0x010408d2, 0x01d8, 0x00000000,
  148. 0x01dc, 0x00000000, 0x01e4, 0x00005fff, 0x01e8, 0x00000000,
  149. 0x01ec, 0x00000000, 0x01f0, 0x00000000, 0x01f4, 0x00000000,
  150. 0x01f8, 0x00000000, 0x01fc, 0x02000000, 0x0200, 0x00000000,
  151. 0x0204, 0x00000000, 0x0208, 0x00000008, 0x020c, 0x0000000d,
  152. 0x41fc, 0x00000000, 0x4400, 0x00000000, 0x4410, 0x00000000,
  153. 0x4420, 0x00000000, 0x4430, 0x00000000, 0x4440, 0x00000000,
  154. 0x4450, 0x00000000, 0x4460, 0x00000000, 0x4470, 0x00000000,
  155. 0xf080, 0x00003018, 0xf084, 0x00000fff, 0xf800, 0x00000000,
  156. 0xf804, 0x00002e00, 0xf8d0, 0x00000001, 0xf8d4, 0x00000000,
  157. 0xff00, 0x00000301, 0xff0c, 0x01000000, 0xffe0, 0x00000000,
  158. 0xfff4, 0x00004011, 0x0090, 0x00000000, 0x0094, 0x00000000,
  159. 0x0098, 0x00000000, 0x009c, 0x3f3f3f3f,
  160. };
  161. static const struct iio_event_spec aw_common_events[3] = {
  162. {
  163. .type = IIO_EV_TYPE_THRESH,
  164. .dir = IIO_EV_DIR_RISING,
  165. .mask_separate = BIT(IIO_EV_INFO_PERIOD),
  166. },
  167. {
  168. .type = IIO_EV_TYPE_THRESH,
  169. .dir = IIO_EV_DIR_FALLING,
  170. .mask_separate = BIT(IIO_EV_INFO_PERIOD),
  171. },
  172. {
  173. .type = IIO_EV_TYPE_THRESH,
  174. .dir = IIO_EV_DIR_EITHER,
  175. .mask_separate = BIT(IIO_EV_INFO_ENABLE) |
  176. BIT(IIO_EV_INFO_HYSTERESIS) |
  177. BIT(IIO_EV_INFO_VALUE),
  178. }
  179. };
  180. #define AW_IIO_CHANNEL(idx) \
  181. { \
  182. .type = IIO_PROXIMITY, \
  183. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  184. .indexed = 1, \
  185. .channel = idx, \
  186. .event_spec = aw_common_events, \
  187. .num_event_specs = ARRAY_SIZE(aw_common_events), \
  188. } \
  189. static const struct iio_chan_spec aw96103_channels[] = {
  190. AW_IIO_CHANNEL(0),
  191. AW_IIO_CHANNEL(1),
  192. AW_IIO_CHANNEL(2),
  193. AW_IIO_CHANNEL(3),
  194. };
  195. static const struct iio_chan_spec aw96105_channels[] = {
  196. AW_IIO_CHANNEL(0),
  197. AW_IIO_CHANNEL(1),
  198. AW_IIO_CHANNEL(2),
  199. AW_IIO_CHANNEL(3),
  200. AW_IIO_CHANNEL(4),
  201. AW_IIO_CHANNEL(5),
  202. };
  203. static const struct aw_chip_info aw_chip_info_tbl[] = {
  204. [AW96103_VAL] = {
  205. .name = "aw96103_sensor",
  206. .channels = aw96103_channels,
  207. .num_channels = ARRAY_SIZE(aw96103_channels),
  208. },
  209. [AW96105_VAL] = {
  210. .name = "aw96105_sensor",
  211. .channels = aw96105_channels,
  212. .num_channels = ARRAY_SIZE(aw96105_channels),
  213. },
  214. };
  215. static void aw96103_parsing_bin_file(struct aw_bin *bin)
  216. {
  217. bin->valid_data_addr = AW96103_BIN_VALID_DATA_OFFSET;
  218. bin->valid_data_len =
  219. *(unsigned int *)(bin->data + AW96103_BIN_DATA_LEN_OFFSET) -
  220. AW96103_BIN_DATA_REG_NUM_SIZE;
  221. memcpy(bin->chip_type, bin->data + AW96103_BIN_CHIP_TYPE_OFFSET,
  222. AW96103_BIN_CHIP_TYPE_SIZE);
  223. }
  224. static const struct regmap_config aw96103_regmap_confg = {
  225. .reg_bits = 16,
  226. .val_bits = 32,
  227. };
  228. static int aw96103_get_diff_raw(struct aw96103 *aw96103, unsigned int chan,
  229. int *buf)
  230. {
  231. u32 data;
  232. int ret;
  233. ret = regmap_read(aw96103->regmap,
  234. AW96103_REG_DIFF_CH0 + chan * 4, &data);
  235. if (ret)
  236. return ret;
  237. *buf = (int)(data / AW_DATA_PROCESS_FACTOR);
  238. return 0;
  239. }
  240. static int aw96103_read_raw(struct iio_dev *indio_dev,
  241. const struct iio_chan_spec *chan,
  242. int *val, int *val2, long mask)
  243. {
  244. struct aw96103 *aw96103 = iio_priv(indio_dev);
  245. int ret;
  246. switch (mask) {
  247. case IIO_CHAN_INFO_RAW:
  248. ret = aw96103_get_diff_raw(aw96103, chan->channel, val);
  249. if (ret)
  250. return ret;
  251. return IIO_VAL_INT;
  252. default:
  253. return -EINVAL;
  254. }
  255. }
  256. static int aw96103_read_thresh(struct aw96103 *aw96103,
  257. const struct iio_chan_spec *chan, int *val)
  258. {
  259. int ret;
  260. ret = regmap_read(aw96103->regmap,
  261. AW96103_REG_PROXTH0_CH(chan->channel), val);
  262. if (ret)
  263. return ret;
  264. return IIO_VAL_INT;
  265. }
  266. static int aw96103_read_out_debounce(struct aw96103 *aw96103,
  267. const struct iio_chan_spec *chan,
  268. int *val)
  269. {
  270. unsigned int reg_val;
  271. int ret;
  272. ret = regmap_read(aw96103->regmap,
  273. AW96103_REG_PROXCTRL_CH(chan->channel), &reg_val);
  274. if (ret)
  275. return ret;
  276. *val = FIELD_GET(AW96103_OUTDEB_MASK, reg_val);
  277. return IIO_VAL_INT;
  278. }
  279. static int aw96103_read_in_debounce(struct aw96103 *aw96103,
  280. const struct iio_chan_spec *chan, int *val)
  281. {
  282. unsigned int reg_val;
  283. int ret;
  284. ret = regmap_read(aw96103->regmap,
  285. AW96103_REG_PROXCTRL_CH(chan->channel), &reg_val);
  286. if (ret)
  287. return ret;
  288. *val = FIELD_GET(AW96103_INDEB_MASK, reg_val);
  289. return IIO_VAL_INT;
  290. }
  291. static int aw96103_read_hysteresis(struct aw96103 *aw96103,
  292. const struct iio_chan_spec *chan, int *val)
  293. {
  294. unsigned int reg_val;
  295. int ret;
  296. ret = regmap_read(aw96103->regmap,
  297. AW96103_REG_PROXCTRL_CH(chan->channel), &reg_val);
  298. if (ret)
  299. return ret;
  300. *val = FIELD_GET(AW96103_THHYST_MASK, reg_val);
  301. return IIO_VAL_INT;
  302. }
  303. static int aw96103_read_event_val(struct iio_dev *indio_dev,
  304. const struct iio_chan_spec *chan,
  305. enum iio_event_type type,
  306. enum iio_event_direction dir,
  307. enum iio_event_info info,
  308. int *val, int *val2)
  309. {
  310. struct aw96103 *aw96103 = iio_priv(indio_dev);
  311. if (chan->type != IIO_PROXIMITY)
  312. return -EINVAL;
  313. switch (info) {
  314. case IIO_EV_INFO_VALUE:
  315. return aw96103_read_thresh(aw96103, chan, val);
  316. case IIO_EV_INFO_PERIOD:
  317. switch (dir) {
  318. case IIO_EV_DIR_RISING:
  319. return aw96103_read_out_debounce(aw96103, chan, val);
  320. case IIO_EV_DIR_FALLING:
  321. return aw96103_read_in_debounce(aw96103, chan, val);
  322. default:
  323. return -EINVAL;
  324. }
  325. case IIO_EV_INFO_HYSTERESIS:
  326. return aw96103_read_hysteresis(aw96103, chan, val);
  327. default:
  328. return -EINVAL;
  329. }
  330. }
  331. static int aw96103_write_event_val(struct iio_dev *indio_dev,
  332. const struct iio_chan_spec *chan,
  333. enum iio_event_type type,
  334. enum iio_event_direction dir,
  335. enum iio_event_info info, int val, int val2)
  336. {
  337. struct aw96103 *aw96103 = iio_priv(indio_dev);
  338. if (chan->type != IIO_PROXIMITY)
  339. return -EINVAL;
  340. switch (info) {
  341. case IIO_EV_INFO_VALUE:
  342. return regmap_write(aw96103->regmap,
  343. AW96103_REG_PROXTH0_CH(chan->channel), val);
  344. case IIO_EV_INFO_PERIOD:
  345. switch (dir) {
  346. case IIO_EV_DIR_RISING:
  347. return regmap_update_bits(aw96103->regmap,
  348. AW96103_REG_PROXCTRL_CH(chan->channel),
  349. AW96103_OUTDEB_MASK,
  350. FIELD_PREP(AW96103_OUTDEB_MASK, val));
  351. case IIO_EV_DIR_FALLING:
  352. return regmap_update_bits(aw96103->regmap,
  353. AW96103_REG_PROXCTRL_CH(chan->channel),
  354. AW96103_INDEB_MASK,
  355. FIELD_PREP(AW96103_INDEB_MASK, val));
  356. default:
  357. return -EINVAL;
  358. }
  359. case IIO_EV_INFO_HYSTERESIS:
  360. return regmap_update_bits(aw96103->regmap,
  361. AW96103_REG_PROXCTRL_CH(chan->channel),
  362. AW96103_THHYST_MASK,
  363. FIELD_PREP(AW96103_THHYST_MASK, val));
  364. default:
  365. return -EINVAL;
  366. }
  367. }
  368. static int aw96103_read_event_config(struct iio_dev *indio_dev,
  369. const struct iio_chan_spec *chan,
  370. enum iio_event_type type,
  371. enum iio_event_direction dir)
  372. {
  373. struct aw96103 *aw96103 = iio_priv(indio_dev);
  374. return aw96103->channels_arr[chan->channel].used;
  375. }
  376. static int aw96103_write_event_config(struct iio_dev *indio_dev,
  377. const struct iio_chan_spec *chan,
  378. enum iio_event_type type,
  379. enum iio_event_direction dir, int state)
  380. {
  381. struct aw96103 *aw96103 = iio_priv(indio_dev);
  382. aw96103->channels_arr[chan->channel].used = !!state;
  383. return regmap_update_bits(aw96103->regmap, AW96103_REG_SCANCTRL0,
  384. BIT(chan->channel),
  385. state ? BIT(chan->channel) : 0);
  386. }
  387. static struct iio_info iio_info = {
  388. .read_raw = aw96103_read_raw,
  389. .read_event_value = aw96103_read_event_val,
  390. .write_event_value = aw96103_write_event_val,
  391. .read_event_config = aw96103_read_event_config,
  392. .write_event_config = aw96103_write_event_config,
  393. };
  394. static int aw96103_channel_scan_start(struct aw96103 *aw96103)
  395. {
  396. int ret;
  397. ret = regmap_write(aw96103->regmap, AW96103_REG_CMD,
  398. AW96103_ACTIVE_MODE);
  399. if (ret)
  400. return ret;
  401. return regmap_write(aw96103->regmap, AW96103_REG_IRQEN,
  402. aw96103->hostirqen);
  403. }
  404. static int aw96103_reg_version_comp(struct aw96103 *aw96103,
  405. struct aw_bin *aw_bin)
  406. {
  407. u32 blfilt1_data, fw_ver;
  408. unsigned char i;
  409. int ret;
  410. ret = regmap_read(aw96103->regmap, AW96103_REG_FWVER2, &fw_ver);
  411. if (ret)
  412. return ret;
  413. /*
  414. * If the chip version is AW96103A and the loaded register
  415. * configuration file is for AW96103, special handling of the
  416. * AW96103_REG_BLRSTRNG_CH0 register is required.
  417. */
  418. if ((fw_ver != AW96103A) || (aw_bin->chip_type[7] != '\0'))
  419. return 0;
  420. for (i = 0; i < aw96103->max_channels; i++) {
  421. ret = regmap_read(aw96103->regmap,
  422. AW96103_REG_BLFILT_CH0 + (AW96103_BLFILT_CH_STEP * i),
  423. &blfilt1_data);
  424. if (ret)
  425. return ret;
  426. if (FIELD_GET(AW96103_BLERRTRIG_MASK, blfilt1_data) != 1)
  427. return 0;
  428. ret = regmap_update_bits(aw96103->regmap,
  429. AW96103_REG_BLRSTRNG_CH0 + (AW96103_BLFILT_CH_STEP * i),
  430. AW96103_BLRSTRNG_MASK, 1 << i);
  431. if (ret)
  432. return ret;
  433. }
  434. return 0;
  435. }
  436. static int aw96103_bin_valid_loaded(struct aw96103 *aw96103,
  437. struct aw_bin *aw_bin_data_s)
  438. {
  439. unsigned int start_addr = aw_bin_data_s->valid_data_addr;
  440. u32 i, reg_data;
  441. u16 reg_addr;
  442. int ret;
  443. for (i = 0; i < aw_bin_data_s->valid_data_len;
  444. i += 6, start_addr += 6) {
  445. reg_addr = get_unaligned_le16(aw_bin_data_s->data + start_addr);
  446. reg_data = get_unaligned_le32(aw_bin_data_s->data +
  447. start_addr + 2);
  448. if ((reg_addr == AW96103_REG_EEDA0) ||
  449. (reg_addr == AW96103_REG_EEDA1))
  450. continue;
  451. if (reg_addr == AW96103_REG_IRQEN) {
  452. aw96103->hostirqen = reg_data;
  453. continue;
  454. }
  455. if (reg_addr == AW96103_REG_SCANCTRL0)
  456. aw96103->chan_en = FIELD_GET(AW96103_CHAN_EN_MASK,
  457. reg_data);
  458. ret = regmap_write(aw96103->regmap, reg_addr, reg_data);
  459. if (ret < 0)
  460. return ret;
  461. }
  462. ret = aw96103_reg_version_comp(aw96103, aw_bin_data_s);
  463. if (ret)
  464. return ret;
  465. return aw96103_channel_scan_start(aw96103);
  466. }
  467. static int aw96103_para_loaded(struct aw96103 *aw96103)
  468. {
  469. int i, ret;
  470. for (i = 0; i < ARRAY_SIZE(aw96103_reg_default); i += 2) {
  471. ret = regmap_write(aw96103->regmap,
  472. (u16)aw96103_reg_default[i],
  473. (u32)aw96103_reg_default[i + 1]);
  474. if (ret)
  475. return ret;
  476. if (aw96103_reg_default[i] == AW96103_REG_IRQEN)
  477. aw96103->hostirqen = aw96103_reg_default[i + 1];
  478. else if (aw96103_reg_default[i] == AW96103_REG_SCANCTRL0)
  479. aw96103->chan_en = FIELD_GET(AW96103_CHAN_EN_MASK,
  480. aw96103_reg_default[i + 1]);
  481. }
  482. return aw96103_channel_scan_start(aw96103);
  483. }
  484. static int aw96103_cfg_all_loaded(const struct firmware *cont,
  485. struct aw96103 *aw96103)
  486. {
  487. if (!cont)
  488. return -EINVAL;
  489. struct aw_bin *aw_bin __free(kfree) =
  490. kzalloc(cont->size + sizeof(*aw_bin), GFP_KERNEL);
  491. if (!aw_bin)
  492. return -ENOMEM;
  493. aw_bin->len = cont->size;
  494. memcpy(aw_bin->data, cont->data, cont->size);
  495. release_firmware(cont);
  496. aw96103_parsing_bin_file(aw_bin);
  497. return aw96103_bin_valid_loaded(aw96103, aw_bin);
  498. }
  499. static void aw96103_cfg_update(const struct firmware *fw, void *data)
  500. {
  501. struct aw96103 *aw96103 = data;
  502. int ret, i;
  503. if (!fw || !fw->data) {
  504. dev_err(aw96103->dev, "No firmware.\n");
  505. return;
  506. }
  507. ret = aw96103_cfg_all_loaded(fw, aw96103);
  508. /*
  509. * If loading the register configuration file fails,
  510. * load the default register configuration in the driver to
  511. * ensure the basic functionality of the device.
  512. */
  513. if (ret) {
  514. ret = aw96103_para_loaded(aw96103);
  515. if (ret) {
  516. dev_err(aw96103->dev, "load param error.\n");
  517. return;
  518. }
  519. }
  520. for (i = 0; i < aw96103->max_channels; i++) {
  521. if ((aw96103->chan_en >> i) & 0x01)
  522. aw96103->channels_arr[i].used = true;
  523. else
  524. aw96103->channels_arr[i].used = false;
  525. }
  526. }
  527. static int aw96103_sw_reset(struct aw96103 *aw96103)
  528. {
  529. int ret;
  530. ret = regmap_write(aw96103->regmap, AW96103_REG_RESET, 0);
  531. /*
  532. * After reset, the initialization process starts to perform and
  533. * it will last for a bout 20ms.
  534. */
  535. msleep(20);
  536. return ret;
  537. }
  538. enum aw96103_irq_trigger_position {
  539. FAR = 0,
  540. TRIGGER_TH0 = 0x01,
  541. TRIGGER_TH1 = 0x03,
  542. TRIGGER_TH2 = 0x07,
  543. TRIGGER_TH3 = 0x0f,
  544. };
  545. static irqreturn_t aw96103_irq(int irq, void *data)
  546. {
  547. unsigned int irq_status, curr_status_val, curr_status;
  548. struct iio_dev *indio_dev = data;
  549. struct aw96103 *aw96103 = iio_priv(indio_dev);
  550. int ret, i;
  551. ret = regmap_read(aw96103->regmap, AW96103_REG_IRQSRC, &irq_status);
  552. if (ret)
  553. return IRQ_HANDLED;
  554. ret = regmap_read(aw96103->regmap, AW96103_REG_STAT0, &curr_status_val);
  555. if (ret)
  556. return IRQ_HANDLED;
  557. /*
  558. * Iteratively analyze the interrupt status of different channels,
  559. * with each channel having 4 interrupt states.
  560. */
  561. for (i = 0; i < aw96103->max_channels; i++) {
  562. if (!aw96103->channels_arr[i].used)
  563. continue;
  564. curr_status = (((curr_status_val >> (24 + i)) & 0x1)) |
  565. (((curr_status_val >> (16 + i)) & 0x1) << 1) |
  566. (((curr_status_val >> (8 + i)) & 0x1) << 2) |
  567. (((curr_status_val >> i) & 0x1) << 3);
  568. if (aw96103->channels_arr[i].old_irq_status == curr_status)
  569. continue;
  570. switch (curr_status) {
  571. case FAR:
  572. iio_push_event(indio_dev,
  573. IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, i,
  574. IIO_EV_TYPE_THRESH,
  575. IIO_EV_DIR_RISING),
  576. iio_get_time_ns(indio_dev));
  577. break;
  578. case TRIGGER_TH0:
  579. case TRIGGER_TH1:
  580. case TRIGGER_TH2:
  581. case TRIGGER_TH3:
  582. iio_push_event(indio_dev,
  583. IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, i,
  584. IIO_EV_TYPE_THRESH,
  585. IIO_EV_DIR_FALLING),
  586. iio_get_time_ns(indio_dev));
  587. break;
  588. default:
  589. return IRQ_HANDLED;
  590. }
  591. aw96103->channels_arr[i].old_irq_status = curr_status;
  592. }
  593. return IRQ_HANDLED;
  594. }
  595. static int aw96103_interrupt_init(struct iio_dev *indio_dev,
  596. struct i2c_client *i2c)
  597. {
  598. struct aw96103 *aw96103 = iio_priv(indio_dev);
  599. unsigned int irq_status;
  600. int ret;
  601. ret = regmap_write(aw96103->regmap, AW96103_REG_IRQEN, 0);
  602. if (ret)
  603. return ret;
  604. ret = regmap_read(aw96103->regmap, AW96103_REG_IRQSRC, &irq_status);
  605. if (ret)
  606. return ret;
  607. ret = devm_request_threaded_irq(aw96103->dev, i2c->irq, NULL,
  608. aw96103_irq, IRQF_ONESHOT,
  609. "aw96103_irq", indio_dev);
  610. if (ret)
  611. return ret;
  612. return regmap_write(aw96103->regmap, AW96103_REG_IRQEN,
  613. aw96103->hostirqen);
  614. }
  615. static int aw96103_wait_chip_init(struct aw96103 *aw96103)
  616. {
  617. unsigned int cnt = 20;
  618. u32 reg_data;
  619. int ret;
  620. while (cnt--) {
  621. /*
  622. * The device should generate an initialization completion
  623. * interrupt within 20ms.
  624. */
  625. ret = regmap_read(aw96103->regmap, AW96103_REG_IRQSRC,
  626. &reg_data);
  627. if (ret)
  628. return ret;
  629. if (FIELD_GET(AW96103_INITOVERIRQ_MASK, reg_data))
  630. return 0;
  631. fsleep(1000);
  632. }
  633. return -ETIMEDOUT;
  634. }
  635. static int aw96103_read_chipid(struct aw96103 *aw96103)
  636. {
  637. unsigned char cnt = 0;
  638. u32 reg_val = 0;
  639. int ret;
  640. while (cnt < 3) {
  641. /*
  642. * This retry mechanism and the subsequent delay are just
  643. * attempts to read the chip ID as much as possible,
  644. * preventing occasional communication failures from causing
  645. * the chip ID read to fail.
  646. */
  647. ret = regmap_read(aw96103->regmap, AW96103_REG_CHIPID,
  648. &reg_val);
  649. if (ret < 0) {
  650. cnt++;
  651. fsleep(2000);
  652. continue;
  653. }
  654. break;
  655. }
  656. if (cnt == 3)
  657. return -ETIMEDOUT;
  658. if (FIELD_GET(AW96103_CHIPID_MASK, reg_val) != AW96103_CHIP_ID)
  659. dev_info(aw96103->dev,
  660. "unexpected chipid, id=0x%08X\n", reg_val);
  661. return 0;
  662. }
  663. static int aw96103_i2c_probe(struct i2c_client *i2c)
  664. {
  665. const struct aw_chip_info *chip_info;
  666. struct iio_dev *indio_dev;
  667. struct aw96103 *aw96103;
  668. int ret;
  669. indio_dev = devm_iio_device_alloc(&i2c->dev, sizeof(*aw96103));
  670. if (!indio_dev)
  671. return -ENOMEM;
  672. aw96103 = iio_priv(indio_dev);
  673. aw96103->dev = &i2c->dev;
  674. chip_info = i2c_get_match_data(i2c);
  675. aw96103->max_channels = chip_info->num_channels;
  676. aw96103->regmap = devm_regmap_init_i2c(i2c, &aw96103_regmap_confg);
  677. if (IS_ERR(aw96103->regmap))
  678. return PTR_ERR(aw96103->regmap);
  679. ret = devm_regulator_get_enable(aw96103->dev, "vcc");
  680. if (ret < 0)
  681. return ret;
  682. ret = aw96103_read_chipid(aw96103);
  683. if (ret)
  684. return ret;
  685. ret = aw96103_sw_reset(aw96103);
  686. if (ret)
  687. return ret;
  688. ret = aw96103_wait_chip_init(aw96103);
  689. if (ret)
  690. return ret;
  691. ret = request_firmware_nowait(THIS_MODULE, true, "aw96103_0.bin",
  692. aw96103->dev, GFP_KERNEL, aw96103,
  693. aw96103_cfg_update);
  694. if (ret)
  695. return ret;
  696. ret = aw96103_interrupt_init(indio_dev, i2c);
  697. if (ret)
  698. return ret;
  699. indio_dev->modes = INDIO_DIRECT_MODE;
  700. indio_dev->num_channels = chip_info->num_channels;
  701. indio_dev->channels = chip_info->channels;
  702. indio_dev->info = &iio_info;
  703. indio_dev->name = chip_info->name;
  704. return devm_iio_device_register(aw96103->dev, indio_dev);
  705. }
  706. static const struct of_device_id aw96103_dt_match[] = {
  707. {
  708. .compatible = "awinic,aw96103",
  709. .data = &aw_chip_info_tbl[AW96103_VAL]
  710. },
  711. {
  712. .compatible = "awinic,aw96105",
  713. .data = &aw_chip_info_tbl[AW96105_VAL]
  714. },
  715. { }
  716. };
  717. MODULE_DEVICE_TABLE(of, aw96103_dt_match);
  718. static const struct i2c_device_id aw96103_i2c_id[] = {
  719. { "aw96103", (kernel_ulong_t)&aw_chip_info_tbl[AW96103_VAL] },
  720. { "aw96105", (kernel_ulong_t)&aw_chip_info_tbl[AW96105_VAL] },
  721. { }
  722. };
  723. MODULE_DEVICE_TABLE(i2c, aw96103_i2c_id);
  724. static struct i2c_driver aw96103_i2c_driver = {
  725. .driver = {
  726. .name = "aw96103_sensor",
  727. .of_match_table = aw96103_dt_match,
  728. },
  729. .probe = aw96103_i2c_probe,
  730. .id_table = aw96103_i2c_id,
  731. };
  732. module_i2c_driver(aw96103_i2c_driver);
  733. MODULE_AUTHOR("Wang Shuaijie <wangshuaijie@awinic.com>");
  734. MODULE_DESCRIPTION("Driver for Awinic AW96103 proximity sensor");
  735. MODULE_LICENSE("GPL v2");