pac1934.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * IIO driver for PAC1934 Multi-Channel DC Power/Energy Monitor
  4. *
  5. * Copyright (C) 2017-2024 Microchip Technology Inc. and its subsidiaries
  6. *
  7. * Author: Bogdan Bolocan <bogdan.bolocan@microchip.com>
  8. * Author: Victor Tudose
  9. * Author: Marius Cristea <marius.cristea@microchip.com>
  10. *
  11. * Datasheet for PAC1931, PAC1932, PAC1933 and PAC1934 can be found here:
  12. * https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ProductDocuments/DataSheets/PAC1931-Family-Data-Sheet-DS20005850E.pdf
  13. */
  14. #include <linux/acpi.h>
  15. #include <linux/bitfield.h>
  16. #include <linux/delay.h>
  17. #include <linux/device.h>
  18. #include <linux/i2c.h>
  19. #include <linux/iio/iio.h>
  20. #include <linux/iio/sysfs.h>
  21. #include <linux/unaligned.h>
  22. /*
  23. * maximum accumulation time should be (17 * 60 * 1000) around 17 minutes@1024 sps
  24. * till PAC1934 accumulation registers starts to saturate
  25. */
  26. #define PAC1934_MAX_RFSH_LIMIT_MS 60000
  27. /* 50msec is the timeout for validity of the cached registers */
  28. #define PAC1934_MIN_POLLING_TIME_MS 50
  29. /*
  30. * 1000usec is the minimum wait time for normal conversions when sample
  31. * rate doesn't change
  32. */
  33. #define PAC1934_MIN_UPDATE_WAIT_TIME_US 1000
  34. /* 32000mV */
  35. #define PAC1934_VOLTAGE_MILLIVOLTS_MAX 32000
  36. /* voltage bits resolution when set for unsigned values */
  37. #define PAC1934_VOLTAGE_U_RES 16
  38. /* voltage bits resolution when set for signed values */
  39. #define PAC1934_VOLTAGE_S_RES 15
  40. /*
  41. * max signed value that can be stored on 32 bits and 8 digits fractional value
  42. * (2^31 - 1) * 10^8 + 99999999
  43. */
  44. #define PAC_193X_MAX_POWER_ACC 214748364799999999LL
  45. /*
  46. * min signed value that can be stored on 32 bits and 8 digits fractional value
  47. * -(2^31) * 10^8 - 99999999
  48. */
  49. #define PAC_193X_MIN_POWER_ACC -214748364899999999LL
  50. #define PAC1934_MAX_NUM_CHANNELS 4
  51. #define PAC1934_MEAS_REG_LEN 76
  52. #define PAC1934_CTRL_REG_LEN 12
  53. #define PAC1934_DEFAULT_CHIP_SAMP_SPEED_HZ 1024
  54. /* I2C address map */
  55. #define PAC1934_REFRESH_REG_ADDR 0x00
  56. #define PAC1934_CTRL_REG_ADDR 0x01
  57. #define PAC1934_ACC_COUNT_REG_ADDR 0x02
  58. #define PAC1934_VPOWER_ACC_1_ADDR 0x03
  59. #define PAC1934_VPOWER_ACC_2_ADDR 0x04
  60. #define PAC1934_VPOWER_ACC_3_ADDR 0x05
  61. #define PAC1934_VPOWER_ACC_4_ADDR 0x06
  62. #define PAC1934_VBUS_1_ADDR 0x07
  63. #define PAC1934_VBUS_2_ADDR 0x08
  64. #define PAC1934_VBUS_3_ADDR 0x09
  65. #define PAC1934_VBUS_4_ADDR 0x0A
  66. #define PAC1934_VSENSE_1_ADDR 0x0B
  67. #define PAC1934_VSENSE_2_ADDR 0x0C
  68. #define PAC1934_VSENSE_3_ADDR 0x0D
  69. #define PAC1934_VSENSE_4_ADDR 0x0E
  70. #define PAC1934_VBUS_AVG_1_ADDR 0x0F
  71. #define PAC1934_VBUS_AVG_2_ADDR 0x10
  72. #define PAC1934_VBUS_AVG_3_ADDR 0x11
  73. #define PAC1934_VBUS_AVG_4_ADDR 0x12
  74. #define PAC1934_VSENSE_AVG_1_ADDR 0x13
  75. #define PAC1934_VSENSE_AVG_2_ADDR 0x14
  76. #define PAC1934_VSENSE_AVG_3_ADDR 0x15
  77. #define PAC1934_VSENSE_AVG_4_ADDR 0x16
  78. #define PAC1934_VPOWER_1_ADDR 0x17
  79. #define PAC1934_VPOWER_2_ADDR 0x18
  80. #define PAC1934_VPOWER_3_ADDR 0x19
  81. #define PAC1934_VPOWER_4_ADDR 0x1A
  82. #define PAC1934_REFRESH_V_REG_ADDR 0x1F
  83. #define PAC1934_SLOW_REG_ADDR 0x20
  84. #define PAC1934_CTRL_STAT_REGS_ADDR 0x1C
  85. #define PAC1934_PID_REG_ADDR 0xFD
  86. #define PAC1934_MID_REG_ADDR 0xFE
  87. #define PAC1934_RID_REG_ADDR 0xFF
  88. /* PRODUCT ID REGISTER + MANUFACTURER ID REGISTER + REVISION ID REGISTER */
  89. #define PAC1934_ID_REG_LEN 3
  90. #define PAC1934_PID_IDX 0
  91. #define PAC1934_MID_IDX 1
  92. #define PAC1934_RID_IDX 2
  93. #define PAC1934_ACPI_GET_NAMES_AND_MOHMS_VALS 1
  94. #define PAC1934_ACPI_GET_UOHMS_VALS 2
  95. #define PAC1934_ACPI_GET_BIPOLAR_SETTINGS 4
  96. #define PAC1934_ACPI_GET_SAMP 5
  97. #define PAC1934_SAMPLE_RATE_SHIFT 6
  98. #define PAC1934_VBUS_SENSE_REG_LEN 2
  99. #define PAC1934_ACC_REG_LEN 3
  100. #define PAC1934_VPOWER_REG_LEN 4
  101. #define PAC1934_VPOWER_ACC_REG_LEN 6
  102. #define PAC1934_MAX_REGISTER_LENGTH 6
  103. #define PAC1934_CUSTOM_ATTR_FOR_CHANNEL 1
  104. /*
  105. * relative offsets when using multi-byte reads/writes even though these
  106. * bytes are read one after the other, they are not at adjacent memory
  107. * locations within the I2C memory map. The chip can skip some addresses
  108. */
  109. #define PAC1934_CHANNEL_DIS_REG_OFF 0
  110. #define PAC1934_NEG_PWR_REG_OFF 1
  111. /*
  112. * when reading/writing multiple bytes from offset PAC1934_CHANNEL_DIS_REG_OFF,
  113. * the chip jumps over the 0x1E (REFRESH_G) and 0x1F (REFRESH_V) offsets
  114. */
  115. #define PAC1934_SLOW_REG_OFF 2
  116. #define PAC1934_CTRL_ACT_REG_OFF 3
  117. #define PAC1934_CHANNEL_DIS_ACT_REG_OFF 4
  118. #define PAC1934_NEG_PWR_ACT_REG_OFF 5
  119. #define PAC1934_CTRL_LAT_REG_OFF 6
  120. #define PAC1934_CHANNEL_DIS_LAT_REG_OFF 7
  121. #define PAC1934_NEG_PWR_LAT_REG_OFF 8
  122. #define PAC1934_PID_REG_OFF 9
  123. #define PAC1934_MID_REG_OFF 10
  124. #define PAC1934_REV_REG_OFF 11
  125. #define PAC1934_CTRL_STATUS_INFO_LEN 12
  126. #define PAC1934_MID 0x5D
  127. #define PAC1931_PID 0x58
  128. #define PAC1932_PID 0x59
  129. #define PAC1933_PID 0x5A
  130. #define PAC1934_PID 0x5B
  131. /* Scale constant = (10^3 * 3.2 * 10^9 / 2^28) for mili Watt-second */
  132. #define PAC1934_SCALE_CONSTANT 11921
  133. #define PAC1934_MAX_VPOWER_RSHIFTED_BY_28B 11921
  134. #define PAC1934_MAX_VSENSE_RSHIFTED_BY_16B 1525
  135. #define PAC1934_DEV_ATTR(name) (&iio_dev_attr_##name.dev_attr.attr)
  136. #define PAC1934_CRTL_SAMPLE_RATE_MASK GENMASK(7, 6)
  137. #define PAC1934_CHAN_SLEEP_MASK BIT(5)
  138. #define PAC1934_CHAN_SLEEP_SET BIT(5)
  139. #define PAC1934_CHAN_SINGLE_MASK BIT(4)
  140. #define PAC1934_CHAN_SINGLE_SHOT_SET BIT(4)
  141. #define PAC1934_CHAN_ALERT_MASK BIT(3)
  142. #define PAC1934_CHAN_ALERT_EN BIT(3)
  143. #define PAC1934_CHAN_ALERT_CC_MASK BIT(2)
  144. #define PAC1934_CHAN_ALERT_CC_EN BIT(2)
  145. #define PAC1934_CHAN_OVF_ALERT_MASK BIT(1)
  146. #define PAC1934_CHAN_OVF_ALERT_EN BIT(1)
  147. #define PAC1934_CHAN_OVF_MASK BIT(0)
  148. #define PAC1934_CHAN_DIS_CH1_OFF_MASK BIT(7)
  149. #define PAC1934_CHAN_DIS_CH2_OFF_MASK BIT(6)
  150. #define PAC1934_CHAN_DIS_CH3_OFF_MASK BIT(5)
  151. #define PAC1934_CHAN_DIS_CH4_OFF_MASK BIT(4)
  152. #define PAC1934_SMBUS_TIMEOUT_MASK BIT(3)
  153. #define PAC1934_SMBUS_BYTECOUNT_MASK BIT(2)
  154. #define PAC1934_SMBUS_NO_SKIP_MASK BIT(1)
  155. #define PAC1934_NEG_PWR_CH1_BIDI_MASK BIT(7)
  156. #define PAC1934_NEG_PWR_CH2_BIDI_MASK BIT(6)
  157. #define PAC1934_NEG_PWR_CH3_BIDI_MASK BIT(5)
  158. #define PAC1934_NEG_PWR_CH4_BIDI_MASK BIT(4)
  159. #define PAC1934_NEG_PWR_CH1_BIDV_MASK BIT(3)
  160. #define PAC1934_NEG_PWR_CH2_BIDV_MASK BIT(2)
  161. #define PAC1934_NEG_PWR_CH3_BIDV_MASK BIT(1)
  162. #define PAC1934_NEG_PWR_CH4_BIDV_MASK BIT(0)
  163. /*
  164. * Universal Unique Identifier (UUID),
  165. * 033771E0-1705-47B4-9535-D1BBE14D9A09,
  166. * is reserved to Microchip for the PAC1934.
  167. */
  168. #define PAC1934_DSM_UUID "033771E0-1705-47B4-9535-D1BBE14D9A09"
  169. enum pac1934_ids {
  170. PAC1931,
  171. PAC1932,
  172. PAC1933,
  173. PAC1934
  174. };
  175. enum pac1934_samps {
  176. PAC1934_SAMP_1024SPS,
  177. PAC1934_SAMP_256SPS,
  178. PAC1934_SAMP_64SPS,
  179. PAC1934_SAMP_8SPS
  180. };
  181. /*
  182. * these indexes are exactly describing the element order within a single
  183. * PAC1934 phys channel IIO channel descriptor; see the static const struct
  184. * iio_chan_spec pac1934_single_channel[] declaration
  185. */
  186. enum pac1934_ch_idx {
  187. PAC1934_CH_ENERGY,
  188. PAC1934_CH_POWER,
  189. PAC1934_CH_VOLTAGE,
  190. PAC1934_CH_CURRENT,
  191. PAC1934_CH_VOLTAGE_AVERAGE,
  192. PAC1934_CH_CURRENT_AVERAGE
  193. };
  194. /**
  195. * struct pac1934_features - features of a pac1934 instance
  196. * @phys_channels: number of physical channels supported by the chip
  197. * @name: chip's name
  198. */
  199. struct pac1934_features {
  200. u8 phys_channels;
  201. const char *name;
  202. };
  203. static const unsigned int samp_rate_map_tbl[] = {
  204. [PAC1934_SAMP_1024SPS] = 1024,
  205. [PAC1934_SAMP_256SPS] = 256,
  206. [PAC1934_SAMP_64SPS] = 64,
  207. [PAC1934_SAMP_8SPS] = 8,
  208. };
  209. static const struct pac1934_features pac1934_chip_config[] = {
  210. [PAC1931] = {
  211. .phys_channels = 1,
  212. .name = "pac1931",
  213. },
  214. [PAC1932] = {
  215. .phys_channels = 2,
  216. .name = "pac1932",
  217. },
  218. [PAC1933] = {
  219. .phys_channels = 3,
  220. .name = "pac1933",
  221. },
  222. [PAC1934] = {
  223. .phys_channels = 4,
  224. .name = "pac1934",
  225. },
  226. };
  227. /**
  228. * struct reg_data - data from the registers
  229. * @meas_regs: snapshot of raw measurements registers
  230. * @ctrl_regs: snapshot of control registers
  231. * @energy_sec_acc: snapshot of energy values
  232. * @vpower_acc: accumulated vpower values
  233. * @vpower: snapshot of vpower registers
  234. * @vbus: snapshot of vbus registers
  235. * @vbus_avg: averages of vbus registers
  236. * @vsense: snapshot of vsense registers
  237. * @vsense_avg: averages of vsense registers
  238. * @num_enabled_channels: count of how many chip channels are currently enabled
  239. */
  240. struct reg_data {
  241. u8 meas_regs[PAC1934_MEAS_REG_LEN];
  242. u8 ctrl_regs[PAC1934_CTRL_REG_LEN];
  243. s64 energy_sec_acc[PAC1934_MAX_NUM_CHANNELS];
  244. s64 vpower_acc[PAC1934_MAX_NUM_CHANNELS];
  245. s32 vpower[PAC1934_MAX_NUM_CHANNELS];
  246. s32 vbus[PAC1934_MAX_NUM_CHANNELS];
  247. s32 vbus_avg[PAC1934_MAX_NUM_CHANNELS];
  248. s32 vsense[PAC1934_MAX_NUM_CHANNELS];
  249. s32 vsense_avg[PAC1934_MAX_NUM_CHANNELS];
  250. u8 num_enabled_channels;
  251. };
  252. /**
  253. * struct pac1934_chip_info - information about the chip
  254. * @client: the i2c-client attached to the device
  255. * @lock: synchronize access to driver's state members
  256. * @work_chip_rfsh: work queue used for refresh commands
  257. * @phys_channels: phys channels count
  258. * @active_channels: array of values, true means that channel is active
  259. * @enable_energy: array of values, true means that channel energy is measured
  260. * @bi_dir: array of bools, true means that channel is bidirectional
  261. * @chip_variant: chip variant
  262. * @chip_revision: chip revision
  263. * @shunts: shunts
  264. * @chip_reg_data: chip reg data
  265. * @sample_rate_value: sampling frequency
  266. * @labels: table with channels labels
  267. * @iio_info: iio_info
  268. * @tstamp: chip's uptime
  269. */
  270. struct pac1934_chip_info {
  271. struct i2c_client *client;
  272. struct mutex lock; /* synchronize access to driver's state members */
  273. struct delayed_work work_chip_rfsh;
  274. u8 phys_channels;
  275. bool active_channels[PAC1934_MAX_NUM_CHANNELS];
  276. bool enable_energy[PAC1934_MAX_NUM_CHANNELS];
  277. bool bi_dir[PAC1934_MAX_NUM_CHANNELS];
  278. u8 chip_variant;
  279. u8 chip_revision;
  280. u32 shunts[PAC1934_MAX_NUM_CHANNELS];
  281. struct reg_data chip_reg_data;
  282. s32 sample_rate_value;
  283. char *labels[PAC1934_MAX_NUM_CHANNELS];
  284. struct iio_info iio_info;
  285. unsigned long tstamp;
  286. };
  287. #define TO_PAC1934_CHIP_INFO(d) container_of(d, struct pac1934_chip_info, work_chip_rfsh)
  288. #define PAC1934_VPOWER_ACC_CHANNEL(_index, _si, _address) { \
  289. .type = IIO_ENERGY, \
  290. .address = (_address), \
  291. .indexed = 1, \
  292. .channel = (_index), \
  293. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  294. BIT(IIO_CHAN_INFO_SCALE) | \
  295. BIT(IIO_CHAN_INFO_ENABLE), \
  296. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  297. .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  298. .scan_index = (_si), \
  299. .scan_type = { \
  300. .sign = 'u', \
  301. .realbits = 48, \
  302. .storagebits = 64, \
  303. .endianness = IIO_CPU, \
  304. } \
  305. }
  306. #define PAC1934_VBUS_CHANNEL(_index, _si, _address) { \
  307. .type = IIO_VOLTAGE, \
  308. .address = (_address), \
  309. .indexed = 1, \
  310. .channel = (_index), \
  311. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  312. BIT(IIO_CHAN_INFO_SCALE), \
  313. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  314. .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  315. .scan_index = (_si), \
  316. .scan_type = { \
  317. .sign = 'u', \
  318. .realbits = 16, \
  319. .storagebits = 16, \
  320. .endianness = IIO_CPU, \
  321. } \
  322. }
  323. #define PAC1934_VBUS_AVG_CHANNEL(_index, _si, _address) { \
  324. .type = IIO_VOLTAGE, \
  325. .address = (_address), \
  326. .indexed = 1, \
  327. .channel = (_index), \
  328. .info_mask_separate = BIT(IIO_CHAN_INFO_AVERAGE_RAW) | \
  329. BIT(IIO_CHAN_INFO_SCALE), \
  330. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  331. .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  332. .scan_index = (_si), \
  333. .scan_type = { \
  334. .sign = 'u', \
  335. .realbits = 16, \
  336. .storagebits = 16, \
  337. .endianness = IIO_CPU, \
  338. } \
  339. }
  340. #define PAC1934_VSENSE_CHANNEL(_index, _si, _address) { \
  341. .type = IIO_CURRENT, \
  342. .address = (_address), \
  343. .indexed = 1, \
  344. .channel = (_index), \
  345. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  346. BIT(IIO_CHAN_INFO_SCALE), \
  347. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  348. .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  349. .scan_index = (_si), \
  350. .scan_type = { \
  351. .sign = 'u', \
  352. .realbits = 16, \
  353. .storagebits = 16, \
  354. .endianness = IIO_CPU, \
  355. } \
  356. }
  357. #define PAC1934_VSENSE_AVG_CHANNEL(_index, _si, _address) { \
  358. .type = IIO_CURRENT, \
  359. .address = (_address), \
  360. .indexed = 1, \
  361. .channel = (_index), \
  362. .info_mask_separate = BIT(IIO_CHAN_INFO_AVERAGE_RAW) | \
  363. BIT(IIO_CHAN_INFO_SCALE), \
  364. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  365. .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  366. .scan_index = (_si), \
  367. .scan_type = { \
  368. .sign = 'u', \
  369. .realbits = 16, \
  370. .storagebits = 16, \
  371. .endianness = IIO_CPU, \
  372. } \
  373. }
  374. #define PAC1934_VPOWER_CHANNEL(_index, _si, _address) { \
  375. .type = IIO_POWER, \
  376. .address = (_address), \
  377. .indexed = 1, \
  378. .channel = (_index), \
  379. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  380. BIT(IIO_CHAN_INFO_SCALE), \
  381. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  382. .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  383. .scan_index = (_si), \
  384. .scan_type = { \
  385. .sign = 'u', \
  386. .realbits = 28, \
  387. .storagebits = 32, \
  388. .shift = 4, \
  389. .endianness = IIO_CPU, \
  390. } \
  391. }
  392. static const struct iio_chan_spec pac1934_single_channel[] = {
  393. PAC1934_VPOWER_ACC_CHANNEL(0, 0, PAC1934_VPOWER_ACC_1_ADDR),
  394. PAC1934_VPOWER_CHANNEL(0, 0, PAC1934_VPOWER_1_ADDR),
  395. PAC1934_VBUS_CHANNEL(0, 0, PAC1934_VBUS_1_ADDR),
  396. PAC1934_VSENSE_CHANNEL(0, 0, PAC1934_VSENSE_1_ADDR),
  397. PAC1934_VBUS_AVG_CHANNEL(0, 0, PAC1934_VBUS_AVG_1_ADDR),
  398. PAC1934_VSENSE_AVG_CHANNEL(0, 0, PAC1934_VSENSE_AVG_1_ADDR),
  399. };
  400. /* Low-level I2c functions used to transfer up to 76 bytes at once */
  401. static int pac1934_i2c_read(struct i2c_client *client, u8 reg_addr,
  402. void *databuf, u8 len)
  403. {
  404. int ret;
  405. struct i2c_msg msgs[2] = {
  406. {
  407. .addr = client->addr,
  408. .len = 1,
  409. .buf = (u8 *)&reg_addr,
  410. },
  411. {
  412. .addr = client->addr,
  413. .len = len,
  414. .buf = databuf,
  415. .flags = I2C_M_RD
  416. }
  417. };
  418. ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  419. if (ret < 0)
  420. return ret;
  421. return 0;
  422. }
  423. static int pac1934_get_samp_rate_idx(struct pac1934_chip_info *info,
  424. u32 new_samp_rate)
  425. {
  426. int cnt;
  427. for (cnt = 0; cnt < ARRAY_SIZE(samp_rate_map_tbl); cnt++)
  428. if (new_samp_rate == samp_rate_map_tbl[cnt])
  429. return cnt;
  430. /* not a valid sample rate value */
  431. return -EINVAL;
  432. }
  433. static ssize_t pac1934_shunt_value_show(struct device *dev,
  434. struct device_attribute *attr,
  435. char *buf)
  436. {
  437. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  438. struct pac1934_chip_info *info = iio_priv(indio_dev);
  439. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  440. return sysfs_emit(buf, "%u\n", info->shunts[this_attr->address]);
  441. }
  442. static ssize_t pac1934_shunt_value_store(struct device *dev,
  443. struct device_attribute *attr,
  444. const char *buf, size_t count)
  445. {
  446. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  447. struct pac1934_chip_info *info = iio_priv(indio_dev);
  448. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  449. int sh_val;
  450. if (kstrtouint(buf, 10, &sh_val)) {
  451. dev_err(dev, "Shunt value is not valid\n");
  452. return -EINVAL;
  453. }
  454. scoped_guard(mutex, &info->lock)
  455. info->shunts[this_attr->address] = sh_val;
  456. return count;
  457. }
  458. static int pac1934_read_avail(struct iio_dev *indio_dev,
  459. struct iio_chan_spec const *channel,
  460. const int **vals, int *type, int *length, long mask)
  461. {
  462. switch (mask) {
  463. case IIO_CHAN_INFO_SAMP_FREQ:
  464. *type = IIO_VAL_INT;
  465. *vals = samp_rate_map_tbl;
  466. *length = ARRAY_SIZE(samp_rate_map_tbl);
  467. return IIO_AVAIL_LIST;
  468. }
  469. return -EINVAL;
  470. }
  471. static int pac1934_send_refresh(struct pac1934_chip_info *info,
  472. u8 refresh_cmd, u32 wait_time)
  473. {
  474. /* this function only sends REFRESH or REFRESH_V */
  475. struct i2c_client *client = info->client;
  476. int ret;
  477. u8 bidir_reg;
  478. bool revision_bug = false;
  479. if (info->chip_revision == 2 || info->chip_revision == 3) {
  480. /*
  481. * chip rev 2 and 3 bug workaround
  482. * see: PAC1934 Family Data Sheet Errata DS80000836A.pdf
  483. */
  484. revision_bug = true;
  485. bidir_reg =
  486. FIELD_PREP(PAC1934_NEG_PWR_CH1_BIDI_MASK, info->bi_dir[0]) |
  487. FIELD_PREP(PAC1934_NEG_PWR_CH2_BIDI_MASK, info->bi_dir[1]) |
  488. FIELD_PREP(PAC1934_NEG_PWR_CH3_BIDI_MASK, info->bi_dir[2]) |
  489. FIELD_PREP(PAC1934_NEG_PWR_CH4_BIDI_MASK, info->bi_dir[3]) |
  490. FIELD_PREP(PAC1934_NEG_PWR_CH1_BIDV_MASK, info->bi_dir[0]) |
  491. FIELD_PREP(PAC1934_NEG_PWR_CH2_BIDV_MASK, info->bi_dir[1]) |
  492. FIELD_PREP(PAC1934_NEG_PWR_CH3_BIDV_MASK, info->bi_dir[2]) |
  493. FIELD_PREP(PAC1934_NEG_PWR_CH4_BIDV_MASK, info->bi_dir[3]);
  494. ret = i2c_smbus_write_byte_data(client,
  495. PAC1934_CTRL_STAT_REGS_ADDR +
  496. PAC1934_NEG_PWR_REG_OFF,
  497. bidir_reg);
  498. if (ret)
  499. return ret;
  500. }
  501. ret = i2c_smbus_write_byte(client, refresh_cmd);
  502. if (ret) {
  503. dev_err(&client->dev, "%s - cannot send 0x%02X\n",
  504. __func__, refresh_cmd);
  505. return ret;
  506. }
  507. if (revision_bug) {
  508. /*
  509. * chip rev 2 and 3 bug workaround - write again the same
  510. * register write the updated registers back
  511. */
  512. ret = i2c_smbus_write_byte_data(client,
  513. PAC1934_CTRL_STAT_REGS_ADDR +
  514. PAC1934_NEG_PWR_REG_OFF, bidir_reg);
  515. if (ret)
  516. return ret;
  517. }
  518. /* register data retrieval timestamp */
  519. info->tstamp = jiffies;
  520. /* wait till the data is available */
  521. usleep_range(wait_time, wait_time + 100);
  522. return ret;
  523. }
  524. static int pac1934_reg_snapshot(struct pac1934_chip_info *info,
  525. bool do_refresh, u8 refresh_cmd, u32 wait_time)
  526. {
  527. int ret;
  528. struct i2c_client *client = info->client;
  529. u8 samp_shift, ctrl_regs_tmp;
  530. u8 *offset_reg_data_p;
  531. u16 tmp_value;
  532. u32 samp_rate, cnt, tmp;
  533. s64 curr_energy, inc;
  534. u64 tmp_energy;
  535. struct reg_data *reg_data;
  536. guard(mutex)(&info->lock);
  537. if (do_refresh) {
  538. ret = pac1934_send_refresh(info, refresh_cmd, wait_time);
  539. if (ret < 0) {
  540. dev_err(&client->dev,
  541. "%s - cannot send refresh\n",
  542. __func__);
  543. return ret;
  544. }
  545. }
  546. ret = i2c_smbus_read_i2c_block_data(client, PAC1934_CTRL_STAT_REGS_ADDR,
  547. PAC1934_CTRL_REG_LEN,
  548. (u8 *)info->chip_reg_data.ctrl_regs);
  549. if (ret < 0) {
  550. dev_err(&client->dev,
  551. "%s - cannot read ctrl/status registers\n",
  552. __func__);
  553. return ret;
  554. }
  555. reg_data = &info->chip_reg_data;
  556. /* read the data registers */
  557. ret = pac1934_i2c_read(client, PAC1934_ACC_COUNT_REG_ADDR,
  558. (u8 *)reg_data->meas_regs, PAC1934_MEAS_REG_LEN);
  559. if (ret) {
  560. dev_err(&client->dev,
  561. "%s - cannot read ACC_COUNT register: %d:%d\n",
  562. __func__, ret, PAC1934_MEAS_REG_LEN);
  563. return ret;
  564. }
  565. /* see how much shift is required by the sample rate */
  566. samp_rate = samp_rate_map_tbl[((reg_data->ctrl_regs[PAC1934_CTRL_LAT_REG_OFF]) >> 6)];
  567. samp_shift = get_count_order(samp_rate);
  568. ctrl_regs_tmp = reg_data->ctrl_regs[PAC1934_CHANNEL_DIS_LAT_REG_OFF];
  569. offset_reg_data_p = &reg_data->meas_regs[PAC1934_ACC_REG_LEN];
  570. /* start with VPOWER_ACC */
  571. for (cnt = 0; cnt < info->phys_channels; cnt++) {
  572. /* check if the channel is active, skip all fields if disabled */
  573. if ((ctrl_regs_tmp << cnt) & 0x80)
  574. continue;
  575. /* skip if the energy accumulation is disabled */
  576. if (info->enable_energy[cnt]) {
  577. curr_energy = info->chip_reg_data.energy_sec_acc[cnt];
  578. tmp_energy = get_unaligned_be48(offset_reg_data_p);
  579. if (info->bi_dir[cnt])
  580. reg_data->vpower_acc[cnt] = sign_extend64(tmp_energy, 47);
  581. else
  582. reg_data->vpower_acc[cnt] = tmp_energy;
  583. /*
  584. * compute the scaled to 1 second accumulated energy value;
  585. * energy accumulator scaled to 1sec = VPOWER_ACC/2^samp_shift
  586. * the chip's sampling rate is 2^samp_shift samples/sec
  587. */
  588. inc = (reg_data->vpower_acc[cnt] >> samp_shift);
  589. /* add the power_acc field */
  590. curr_energy += inc;
  591. reg_data->energy_sec_acc[cnt] = clamp(curr_energy,
  592. PAC_193X_MIN_POWER_ACC,
  593. PAC_193X_MAX_POWER_ACC);
  594. }
  595. offset_reg_data_p += PAC1934_VPOWER_ACC_REG_LEN;
  596. }
  597. /* continue with VBUS */
  598. for (cnt = 0; cnt < info->phys_channels; cnt++) {
  599. if ((ctrl_regs_tmp << cnt) & 0x80)
  600. continue;
  601. tmp_value = get_unaligned_be16(offset_reg_data_p);
  602. if (info->bi_dir[cnt])
  603. reg_data->vbus[cnt] = sign_extend32((u32)(tmp_value), 15);
  604. else
  605. reg_data->vbus[cnt] = tmp_value;
  606. offset_reg_data_p += PAC1934_VBUS_SENSE_REG_LEN;
  607. }
  608. /* VSENSE */
  609. for (cnt = 0; cnt < info->phys_channels; cnt++) {
  610. if ((ctrl_regs_tmp << cnt) & 0x80)
  611. continue;
  612. tmp_value = get_unaligned_be16(offset_reg_data_p);
  613. if (info->bi_dir[cnt])
  614. reg_data->vsense[cnt] = sign_extend32((u32)(tmp_value), 15);
  615. else
  616. reg_data->vsense[cnt] = tmp_value;
  617. offset_reg_data_p += PAC1934_VBUS_SENSE_REG_LEN;
  618. }
  619. /* VBUS_AVG */
  620. for (cnt = 0; cnt < info->phys_channels; cnt++) {
  621. if ((ctrl_regs_tmp << cnt) & 0x80)
  622. continue;
  623. tmp_value = get_unaligned_be16(offset_reg_data_p);
  624. if (info->bi_dir[cnt])
  625. reg_data->vbus_avg[cnt] = sign_extend32((u32)(tmp_value), 15);
  626. else
  627. reg_data->vbus_avg[cnt] = tmp_value;
  628. offset_reg_data_p += PAC1934_VBUS_SENSE_REG_LEN;
  629. }
  630. /* VSENSE_AVG */
  631. for (cnt = 0; cnt < info->phys_channels; cnt++) {
  632. if ((ctrl_regs_tmp << cnt) & 0x80)
  633. continue;
  634. tmp_value = get_unaligned_be16(offset_reg_data_p);
  635. if (info->bi_dir[cnt])
  636. reg_data->vsense_avg[cnt] = sign_extend32((u32)(tmp_value), 15);
  637. else
  638. reg_data->vsense_avg[cnt] = tmp_value;
  639. offset_reg_data_p += PAC1934_VBUS_SENSE_REG_LEN;
  640. }
  641. /* VPOWER */
  642. for (cnt = 0; cnt < info->phys_channels; cnt++) {
  643. if ((ctrl_regs_tmp << cnt) & 0x80)
  644. continue;
  645. tmp = get_unaligned_be32(offset_reg_data_p) >> 4;
  646. if (info->bi_dir[cnt])
  647. reg_data->vpower[cnt] = sign_extend32(tmp, 27);
  648. else
  649. reg_data->vpower[cnt] = tmp;
  650. offset_reg_data_p += PAC1934_VPOWER_REG_LEN;
  651. }
  652. return 0;
  653. }
  654. static int pac1934_retrieve_data(struct pac1934_chip_info *info,
  655. u32 wait_time)
  656. {
  657. int ret = 0;
  658. /*
  659. * check if the minimal elapsed time has passed and if so,
  660. * re-read the chip, otherwise the cached info is just fine
  661. */
  662. if (time_after(jiffies, info->tstamp + msecs_to_jiffies(PAC1934_MIN_POLLING_TIME_MS))) {
  663. ret = pac1934_reg_snapshot(info, true, PAC1934_REFRESH_REG_ADDR,
  664. wait_time);
  665. /*
  666. * Re-schedule the work for the read registers on timeout
  667. * (to prevent chip registers saturation)
  668. */
  669. mod_delayed_work(system_percpu_wq, &info->work_chip_rfsh,
  670. msecs_to_jiffies(PAC1934_MAX_RFSH_LIMIT_MS));
  671. }
  672. return ret;
  673. }
  674. static int pac1934_read_raw(struct iio_dev *indio_dev,
  675. struct iio_chan_spec const *chan, int *val,
  676. int *val2, long mask)
  677. {
  678. struct pac1934_chip_info *info = iio_priv(indio_dev);
  679. s64 curr_energy;
  680. int ret, channel = chan->channel - 1;
  681. /*
  682. * For AVG the index should be between 5 to 8.
  683. * To calculate PAC1934_CH_VOLTAGE_AVERAGE,
  684. * respectively PAC1934_CH_CURRENT real index, we need
  685. * to remove the added offset (PAC1934_MAX_NUM_CHANNELS).
  686. */
  687. if (channel >= PAC1934_MAX_NUM_CHANNELS)
  688. channel = channel - PAC1934_MAX_NUM_CHANNELS;
  689. ret = pac1934_retrieve_data(info, PAC1934_MIN_UPDATE_WAIT_TIME_US);
  690. if (ret < 0)
  691. return ret;
  692. switch (mask) {
  693. case IIO_CHAN_INFO_RAW:
  694. switch (chan->type) {
  695. case IIO_VOLTAGE:
  696. *val = info->chip_reg_data.vbus[channel];
  697. return IIO_VAL_INT;
  698. case IIO_CURRENT:
  699. *val = info->chip_reg_data.vsense[channel];
  700. return IIO_VAL_INT;
  701. case IIO_POWER:
  702. *val = info->chip_reg_data.vpower[channel];
  703. return IIO_VAL_INT;
  704. case IIO_ENERGY:
  705. curr_energy = info->chip_reg_data.energy_sec_acc[channel];
  706. *val = (u32)curr_energy;
  707. *val2 = (u32)(curr_energy >> 32);
  708. return IIO_VAL_INT_64;
  709. default:
  710. return -EINVAL;
  711. }
  712. case IIO_CHAN_INFO_AVERAGE_RAW:
  713. switch (chan->type) {
  714. case IIO_VOLTAGE:
  715. *val = info->chip_reg_data.vbus_avg[channel];
  716. return IIO_VAL_INT;
  717. case IIO_CURRENT:
  718. *val = info->chip_reg_data.vsense_avg[channel];
  719. return IIO_VAL_INT;
  720. default:
  721. return -EINVAL;
  722. }
  723. case IIO_CHAN_INFO_SCALE:
  724. switch (chan->address) {
  725. /* Voltages - scale for millivolts */
  726. case PAC1934_VBUS_1_ADDR:
  727. case PAC1934_VBUS_2_ADDR:
  728. case PAC1934_VBUS_3_ADDR:
  729. case PAC1934_VBUS_4_ADDR:
  730. case PAC1934_VBUS_AVG_1_ADDR:
  731. case PAC1934_VBUS_AVG_2_ADDR:
  732. case PAC1934_VBUS_AVG_3_ADDR:
  733. case PAC1934_VBUS_AVG_4_ADDR:
  734. *val = PAC1934_VOLTAGE_MILLIVOLTS_MAX;
  735. if (chan->scan_type.sign == 'u')
  736. *val2 = PAC1934_VOLTAGE_U_RES;
  737. else
  738. *val2 = PAC1934_VOLTAGE_S_RES;
  739. return IIO_VAL_FRACTIONAL_LOG2;
  740. /*
  741. * Currents - scale for mA - depends on the
  742. * channel's shunt value
  743. * (100mV * 1000000) / (2^16 * shunt(uohm))
  744. */
  745. case PAC1934_VSENSE_1_ADDR:
  746. case PAC1934_VSENSE_2_ADDR:
  747. case PAC1934_VSENSE_3_ADDR:
  748. case PAC1934_VSENSE_4_ADDR:
  749. case PAC1934_VSENSE_AVG_1_ADDR:
  750. case PAC1934_VSENSE_AVG_2_ADDR:
  751. case PAC1934_VSENSE_AVG_3_ADDR:
  752. case PAC1934_VSENSE_AVG_4_ADDR:
  753. *val = PAC1934_MAX_VSENSE_RSHIFTED_BY_16B;
  754. if (chan->scan_type.sign == 'u')
  755. *val2 = info->shunts[channel];
  756. else
  757. *val2 = info->shunts[channel] >> 1;
  758. return IIO_VAL_FRACTIONAL;
  759. /*
  760. * Power - uW - it will use the combined scale
  761. * for current and voltage
  762. * current(mA) * voltage(mV) = power (uW)
  763. */
  764. case PAC1934_VPOWER_1_ADDR:
  765. case PAC1934_VPOWER_2_ADDR:
  766. case PAC1934_VPOWER_3_ADDR:
  767. case PAC1934_VPOWER_4_ADDR:
  768. *val = PAC1934_MAX_VPOWER_RSHIFTED_BY_28B;
  769. if (chan->scan_type.sign == 'u')
  770. *val2 = info->shunts[channel];
  771. else
  772. *val2 = info->shunts[channel] >> 1;
  773. return IIO_VAL_FRACTIONAL;
  774. case PAC1934_VPOWER_ACC_1_ADDR:
  775. case PAC1934_VPOWER_ACC_2_ADDR:
  776. case PAC1934_VPOWER_ACC_3_ADDR:
  777. case PAC1934_VPOWER_ACC_4_ADDR:
  778. /*
  779. * expresses the 32 bit scale value here compute
  780. * the scale for energy (miliWatt-second or miliJoule)
  781. */
  782. *val = PAC1934_SCALE_CONSTANT;
  783. if (chan->scan_type.sign == 'u')
  784. *val2 = info->shunts[channel];
  785. else
  786. *val2 = info->shunts[channel] >> 1;
  787. return IIO_VAL_FRACTIONAL;
  788. default:
  789. return -EINVAL;
  790. }
  791. case IIO_CHAN_INFO_SAMP_FREQ:
  792. *val = info->sample_rate_value;
  793. return IIO_VAL_INT;
  794. case IIO_CHAN_INFO_ENABLE:
  795. *val = info->enable_energy[channel];
  796. return IIO_VAL_INT;
  797. default:
  798. return -EINVAL;
  799. }
  800. }
  801. static int pac1934_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
  802. int val, int val2, long mask)
  803. {
  804. struct pac1934_chip_info *info = iio_priv(indio_dev);
  805. struct i2c_client *client = info->client;
  806. int ret = -EINVAL;
  807. s32 old_samp_rate;
  808. u8 ctrl_reg;
  809. switch (mask) {
  810. case IIO_CHAN_INFO_SAMP_FREQ:
  811. ret = pac1934_get_samp_rate_idx(info, val);
  812. if (ret < 0)
  813. return ret;
  814. /* write the new sampling value and trigger a snapshot(incl refresh) */
  815. scoped_guard(mutex, &info->lock) {
  816. ctrl_reg = FIELD_PREP(PAC1934_CRTL_SAMPLE_RATE_MASK, ret);
  817. ret = i2c_smbus_write_byte_data(client, PAC1934_CTRL_REG_ADDR, ctrl_reg);
  818. if (ret) {
  819. dev_err(&client->dev,
  820. "%s - can't update sample rate\n",
  821. __func__);
  822. return ret;
  823. }
  824. }
  825. old_samp_rate = info->sample_rate_value;
  826. info->sample_rate_value = val;
  827. /*
  828. * now, force a snapshot with refresh - call retrieve
  829. * data in order to update the refresh timer
  830. * alter the timestamp in order to force trigger a
  831. * register snapshot and a timestamp update
  832. */
  833. info->tstamp -= msecs_to_jiffies(PAC1934_MIN_POLLING_TIME_MS);
  834. ret = pac1934_retrieve_data(info, (1024 / old_samp_rate) * 1000);
  835. if (ret < 0) {
  836. dev_err(&client->dev,
  837. "%s - cannot snapshot ctrl and measurement regs\n",
  838. __func__);
  839. return ret;
  840. }
  841. return 0;
  842. case IIO_CHAN_INFO_ENABLE:
  843. scoped_guard(mutex, &info->lock) {
  844. info->enable_energy[chan->channel - 1] = val ? true : false;
  845. if (!val)
  846. info->chip_reg_data.energy_sec_acc[chan->channel - 1] = 0;
  847. }
  848. return 0;
  849. default:
  850. return -EINVAL;
  851. }
  852. }
  853. static int pac1934_read_label(struct iio_dev *indio_dev,
  854. struct iio_chan_spec const *chan, char *label)
  855. {
  856. struct pac1934_chip_info *info = iio_priv(indio_dev);
  857. switch (chan->address) {
  858. case PAC1934_VBUS_1_ADDR:
  859. case PAC1934_VBUS_2_ADDR:
  860. case PAC1934_VBUS_3_ADDR:
  861. case PAC1934_VBUS_4_ADDR:
  862. return sysfs_emit(label, "%s_VBUS_%d\n",
  863. info->labels[chan->scan_index],
  864. chan->scan_index + 1);
  865. case PAC1934_VBUS_AVG_1_ADDR:
  866. case PAC1934_VBUS_AVG_2_ADDR:
  867. case PAC1934_VBUS_AVG_3_ADDR:
  868. case PAC1934_VBUS_AVG_4_ADDR:
  869. return sysfs_emit(label, "%s_VBUS_AVG_%d\n",
  870. info->labels[chan->scan_index],
  871. chan->scan_index + 1);
  872. case PAC1934_VSENSE_1_ADDR:
  873. case PAC1934_VSENSE_2_ADDR:
  874. case PAC1934_VSENSE_3_ADDR:
  875. case PAC1934_VSENSE_4_ADDR:
  876. return sysfs_emit(label, "%s_IBUS_%d\n",
  877. info->labels[chan->scan_index],
  878. chan->scan_index + 1);
  879. case PAC1934_VSENSE_AVG_1_ADDR:
  880. case PAC1934_VSENSE_AVG_2_ADDR:
  881. case PAC1934_VSENSE_AVG_3_ADDR:
  882. case PAC1934_VSENSE_AVG_4_ADDR:
  883. return sysfs_emit(label, "%s_IBUS_AVG_%d\n",
  884. info->labels[chan->scan_index],
  885. chan->scan_index + 1);
  886. case PAC1934_VPOWER_1_ADDR:
  887. case PAC1934_VPOWER_2_ADDR:
  888. case PAC1934_VPOWER_3_ADDR:
  889. case PAC1934_VPOWER_4_ADDR:
  890. return sysfs_emit(label, "%s_POWER_%d\n",
  891. info->labels[chan->scan_index],
  892. chan->scan_index + 1);
  893. case PAC1934_VPOWER_ACC_1_ADDR:
  894. case PAC1934_VPOWER_ACC_2_ADDR:
  895. case PAC1934_VPOWER_ACC_3_ADDR:
  896. case PAC1934_VPOWER_ACC_4_ADDR:
  897. return sysfs_emit(label, "%s_ENERGY_%d\n",
  898. info->labels[chan->scan_index],
  899. chan->scan_index + 1);
  900. }
  901. return 0;
  902. }
  903. static void pac1934_work_periodic_rfsh(struct work_struct *work)
  904. {
  905. struct pac1934_chip_info *info = TO_PAC1934_CHIP_INFO((struct delayed_work *)work);
  906. struct device *dev = &info->client->dev;
  907. dev_dbg(dev, "%s - Periodic refresh\n", __func__);
  908. /* do a REFRESH, then read */
  909. pac1934_reg_snapshot(info, true, PAC1934_REFRESH_REG_ADDR,
  910. PAC1934_MIN_UPDATE_WAIT_TIME_US);
  911. schedule_delayed_work(&info->work_chip_rfsh,
  912. msecs_to_jiffies(PAC1934_MAX_RFSH_LIMIT_MS));
  913. }
  914. static int pac1934_read_revision(struct pac1934_chip_info *info, u8 *buf)
  915. {
  916. int ret;
  917. struct i2c_client *client = info->client;
  918. ret = i2c_smbus_read_i2c_block_data(client, PAC1934_PID_REG_ADDR,
  919. PAC1934_ID_REG_LEN,
  920. buf);
  921. if (ret < 0) {
  922. dev_err(&client->dev, "cannot read revision\n");
  923. return ret;
  924. }
  925. return 0;
  926. }
  927. static int pac1934_chip_identify(struct pac1934_chip_info *info)
  928. {
  929. u8 rev_info[PAC1934_ID_REG_LEN];
  930. struct device *dev = &info->client->dev;
  931. int ret = 0;
  932. ret = pac1934_read_revision(info, (u8 *)rev_info);
  933. if (ret)
  934. return ret;
  935. info->chip_variant = rev_info[PAC1934_PID_IDX];
  936. info->chip_revision = rev_info[PAC1934_RID_IDX];
  937. dev_dbg(dev, "Chip variant: 0x%02X\n", info->chip_variant);
  938. dev_dbg(dev, "Chip revision: 0x%02X\n", info->chip_revision);
  939. switch (info->chip_variant) {
  940. case PAC1934_PID:
  941. return PAC1934;
  942. case PAC1933_PID:
  943. return PAC1933;
  944. case PAC1932_PID:
  945. return PAC1932;
  946. case PAC1931_PID:
  947. return PAC1931;
  948. default:
  949. return -EINVAL;
  950. }
  951. }
  952. /*
  953. * documentation related to the ACPI device definition
  954. * https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ApplicationNotes/ApplicationNotes/PAC193X-Integration-Notes-for-Microsoft-Windows-10-and-Windows-11-Driver-Support-DS00002534.pdf
  955. */
  956. static int pac1934_acpi_parse_channel_config(struct i2c_client *client,
  957. struct pac1934_chip_info *info)
  958. {
  959. acpi_handle handle;
  960. union acpi_object *rez;
  961. struct device *dev = &client->dev;
  962. unsigned short bi_dir_mask;
  963. int idx, i;
  964. guid_t guid;
  965. handle = ACPI_HANDLE(dev);
  966. guid_parse(PAC1934_DSM_UUID, &guid);
  967. rez = acpi_evaluate_dsm(handle, &guid, 0, PAC1934_ACPI_GET_NAMES_AND_MOHMS_VALS, NULL);
  968. if (!rez)
  969. return -EINVAL;
  970. for (i = 0; i < rez->package.count; i += 2) {
  971. idx = i / 2;
  972. info->labels[idx] =
  973. devm_kmemdup(dev, rez->package.elements[i].string.pointer,
  974. (size_t)rez->package.elements[i].string.length + 1,
  975. GFP_KERNEL);
  976. info->labels[idx][rez->package.elements[i].string.length] = '\0';
  977. info->shunts[idx] = rez->package.elements[i + 1].integer.value * 1000;
  978. info->active_channels[idx] = (info->shunts[idx] != 0);
  979. }
  980. ACPI_FREE(rez);
  981. rez = acpi_evaluate_dsm(handle, &guid, 1, PAC1934_ACPI_GET_UOHMS_VALS, NULL);
  982. if (!rez) {
  983. /*
  984. * initializing with default values
  985. * we assume all channels are unidirectional(the mask is zero)
  986. * and assign the default sampling rate
  987. */
  988. info->sample_rate_value = PAC1934_DEFAULT_CHIP_SAMP_SPEED_HZ;
  989. return 0;
  990. }
  991. for (i = 0; i < rez->package.count; i++) {
  992. idx = i;
  993. info->shunts[idx] = rez->package.elements[i].integer.value;
  994. info->active_channels[idx] = (info->shunts[idx] != 0);
  995. }
  996. ACPI_FREE(rez);
  997. rez = acpi_evaluate_dsm(handle, &guid, 1, PAC1934_ACPI_GET_BIPOLAR_SETTINGS, NULL);
  998. if (!rez)
  999. return -EINVAL;
  1000. bi_dir_mask = rez->package.elements[0].integer.value;
  1001. info->bi_dir[0] = ((bi_dir_mask & (1 << 3)) | (bi_dir_mask & (1 << 7))) != 0;
  1002. info->bi_dir[1] = ((bi_dir_mask & (1 << 2)) | (bi_dir_mask & (1 << 6))) != 0;
  1003. info->bi_dir[2] = ((bi_dir_mask & (1 << 1)) | (bi_dir_mask & (1 << 5))) != 0;
  1004. info->bi_dir[3] = ((bi_dir_mask & (1 << 0)) | (bi_dir_mask & (1 << 4))) != 0;
  1005. ACPI_FREE(rez);
  1006. rez = acpi_evaluate_dsm(handle, &guid, 1, PAC1934_ACPI_GET_SAMP, NULL);
  1007. if (!rez)
  1008. return -EINVAL;
  1009. info->sample_rate_value = rez->package.elements[0].integer.value;
  1010. ACPI_FREE(rez);
  1011. return 0;
  1012. }
  1013. static int pac1934_fw_parse_channel_config(struct i2c_client *client,
  1014. struct pac1934_chip_info *info)
  1015. {
  1016. struct device *dev = &client->dev;
  1017. unsigned int current_channel;
  1018. int idx, ret;
  1019. info->sample_rate_value = 1024;
  1020. current_channel = 1;
  1021. device_for_each_child_node_scoped(dev, node) {
  1022. ret = fwnode_property_read_u32(node, "reg", &idx);
  1023. if (ret)
  1024. return dev_err_probe(dev, ret,
  1025. "reading invalid channel index\n");
  1026. /* adjust idx to match channel index (1 to 4) from the datasheet */
  1027. idx--;
  1028. if (current_channel >= (info->phys_channels + 1) ||
  1029. idx >= info->phys_channels || idx < 0)
  1030. return dev_err_probe(dev, -EINVAL,
  1031. "%s: invalid channel_index %d value\n",
  1032. fwnode_get_name(node), idx);
  1033. /* enable channel */
  1034. info->active_channels[idx] = true;
  1035. ret = fwnode_property_read_u32(node, "shunt-resistor-micro-ohms",
  1036. &info->shunts[idx]);
  1037. if (ret)
  1038. return dev_err_probe(dev, ret,
  1039. "%s: invalid shunt-resistor value: %d\n",
  1040. fwnode_get_name(node), info->shunts[idx]);
  1041. if (fwnode_property_present(node, "label")) {
  1042. ret = fwnode_property_read_string(node, "label",
  1043. (const char **)&info->labels[idx]);
  1044. if (ret)
  1045. return dev_err_probe(dev, ret,
  1046. "%s: invalid rail-name value\n",
  1047. fwnode_get_name(node));
  1048. }
  1049. info->bi_dir[idx] = fwnode_property_read_bool(node, "bipolar");
  1050. current_channel++;
  1051. }
  1052. return 0;
  1053. }
  1054. static void pac1934_cancel_delayed_work(void *dwork)
  1055. {
  1056. cancel_delayed_work_sync(dwork);
  1057. }
  1058. static int pac1934_chip_configure(struct pac1934_chip_info *info)
  1059. {
  1060. int cnt, ret;
  1061. struct i2c_client *client = info->client;
  1062. u8 regs[PAC1934_CTRL_STATUS_INFO_LEN], idx, ctrl_reg;
  1063. u32 wait_time;
  1064. info->chip_reg_data.num_enabled_channels = 0;
  1065. for (cnt = 0; cnt < info->phys_channels; cnt++) {
  1066. if (info->active_channels[cnt])
  1067. info->chip_reg_data.num_enabled_channels++;
  1068. }
  1069. /*
  1070. * read whatever information was gathered before the driver was loaded
  1071. * establish which channels are enabled/disabled and then establish the
  1072. * information retrieval mode (using SKIP or no).
  1073. * Read the chip ID values
  1074. */
  1075. ret = i2c_smbus_read_i2c_block_data(client, PAC1934_CTRL_STAT_REGS_ADDR,
  1076. ARRAY_SIZE(regs),
  1077. (u8 *)regs);
  1078. if (ret < 0) {
  1079. dev_err_probe(&client->dev, ret,
  1080. "%s - cannot read regs from 0x%02X\n",
  1081. __func__, PAC1934_CTRL_STAT_REGS_ADDR);
  1082. return ret;
  1083. }
  1084. /* write the CHANNEL_DIS and the NEG_PWR registers */
  1085. regs[PAC1934_CHANNEL_DIS_REG_OFF] =
  1086. FIELD_PREP(PAC1934_CHAN_DIS_CH1_OFF_MASK, info->active_channels[0] ? 0 : 1) |
  1087. FIELD_PREP(PAC1934_CHAN_DIS_CH2_OFF_MASK, info->active_channels[1] ? 0 : 1) |
  1088. FIELD_PREP(PAC1934_CHAN_DIS_CH3_OFF_MASK, info->active_channels[2] ? 0 : 1) |
  1089. FIELD_PREP(PAC1934_CHAN_DIS_CH4_OFF_MASK, info->active_channels[3] ? 0 : 1) |
  1090. FIELD_PREP(PAC1934_SMBUS_TIMEOUT_MASK, 0) |
  1091. FIELD_PREP(PAC1934_SMBUS_BYTECOUNT_MASK, 0) |
  1092. FIELD_PREP(PAC1934_SMBUS_NO_SKIP_MASK, 0);
  1093. regs[PAC1934_NEG_PWR_REG_OFF] =
  1094. FIELD_PREP(PAC1934_NEG_PWR_CH1_BIDI_MASK, info->bi_dir[0]) |
  1095. FIELD_PREP(PAC1934_NEG_PWR_CH2_BIDI_MASK, info->bi_dir[1]) |
  1096. FIELD_PREP(PAC1934_NEG_PWR_CH3_BIDI_MASK, info->bi_dir[2]) |
  1097. FIELD_PREP(PAC1934_NEG_PWR_CH4_BIDI_MASK, info->bi_dir[3]) |
  1098. FIELD_PREP(PAC1934_NEG_PWR_CH1_BIDV_MASK, info->bi_dir[0]) |
  1099. FIELD_PREP(PAC1934_NEG_PWR_CH2_BIDV_MASK, info->bi_dir[1]) |
  1100. FIELD_PREP(PAC1934_NEG_PWR_CH3_BIDV_MASK, info->bi_dir[2]) |
  1101. FIELD_PREP(PAC1934_NEG_PWR_CH4_BIDV_MASK, info->bi_dir[3]);
  1102. /* no SLOW triggered REFRESH, clear POR */
  1103. regs[PAC1934_SLOW_REG_OFF] = 0;
  1104. /*
  1105. * Write the three bytes sequentially, as the device does not support
  1106. * block write.
  1107. */
  1108. ret = i2c_smbus_write_byte_data(client, PAC1934_CTRL_STAT_REGS_ADDR,
  1109. regs[PAC1934_CHANNEL_DIS_REG_OFF]);
  1110. if (ret)
  1111. return ret;
  1112. ret = i2c_smbus_write_byte_data(client,
  1113. PAC1934_CTRL_STAT_REGS_ADDR + PAC1934_NEG_PWR_REG_OFF,
  1114. regs[PAC1934_NEG_PWR_REG_OFF]);
  1115. if (ret)
  1116. return ret;
  1117. ret = i2c_smbus_write_byte_data(client, PAC1934_SLOW_REG_ADDR,
  1118. regs[PAC1934_SLOW_REG_OFF]);
  1119. if (ret)
  1120. return ret;
  1121. /* Default sampling rate */
  1122. ctrl_reg = FIELD_PREP(PAC1934_CRTL_SAMPLE_RATE_MASK, PAC1934_SAMP_1024SPS);
  1123. ret = i2c_smbus_write_byte_data(client, PAC1934_CTRL_REG_ADDR, ctrl_reg);
  1124. if (ret)
  1125. return ret;
  1126. /*
  1127. * send a REFRESH to the chip, so the new settings take place
  1128. * as well as resetting the accumulators
  1129. */
  1130. ret = i2c_smbus_write_byte(client, PAC1934_REFRESH_REG_ADDR);
  1131. if (ret) {
  1132. dev_err(&client->dev,
  1133. "%s - cannot send 0x%02X\n",
  1134. __func__, PAC1934_REFRESH_REG_ADDR);
  1135. return ret;
  1136. }
  1137. /*
  1138. * get the current(in the chip) sampling speed and compute the
  1139. * required timeout based on its value
  1140. * the timeout is 1/sampling_speed
  1141. */
  1142. idx = regs[PAC1934_CTRL_ACT_REG_OFF] >> PAC1934_SAMPLE_RATE_SHIFT;
  1143. wait_time = (1024 / samp_rate_map_tbl[idx]) * 1000;
  1144. /*
  1145. * wait the maximum amount of time to be on the safe side
  1146. * the maximum wait time is for 8sps
  1147. */
  1148. usleep_range(wait_time, wait_time + 100);
  1149. INIT_DELAYED_WORK(&info->work_chip_rfsh, pac1934_work_periodic_rfsh);
  1150. /* Setup the latest moment for reading the regs before saturation */
  1151. schedule_delayed_work(&info->work_chip_rfsh,
  1152. msecs_to_jiffies(PAC1934_MAX_RFSH_LIMIT_MS));
  1153. return devm_add_action_or_reset(&client->dev, pac1934_cancel_delayed_work,
  1154. &info->work_chip_rfsh);
  1155. }
  1156. static int pac1934_prep_iio_channels(struct pac1934_chip_info *info, struct iio_dev *indio_dev)
  1157. {
  1158. struct iio_chan_spec *ch_sp;
  1159. int channel_size, attribute_count, cnt;
  1160. void *dyn_ch_struct, *tmp_data;
  1161. struct device *dev = &info->client->dev;
  1162. /* find out dynamically how many IIO channels we need */
  1163. attribute_count = 0;
  1164. channel_size = 0;
  1165. for (cnt = 0; cnt < info->phys_channels; cnt++) {
  1166. if (!info->active_channels[cnt])
  1167. continue;
  1168. /* add the size of the properties of one chip physical channel */
  1169. channel_size += sizeof(pac1934_single_channel);
  1170. /* count how many enabled channels we have */
  1171. attribute_count += ARRAY_SIZE(pac1934_single_channel);
  1172. dev_dbg(dev, ":%s: Channel %d active\n", __func__, cnt + 1);
  1173. }
  1174. dyn_ch_struct = devm_kzalloc(dev, channel_size, GFP_KERNEL);
  1175. if (!dyn_ch_struct)
  1176. return -EINVAL;
  1177. tmp_data = dyn_ch_struct;
  1178. /* populate the dynamic channels and make all the adjustments */
  1179. for (cnt = 0; cnt < info->phys_channels; cnt++) {
  1180. if (!info->active_channels[cnt])
  1181. continue;
  1182. memcpy(tmp_data, pac1934_single_channel, sizeof(pac1934_single_channel));
  1183. ch_sp = (struct iio_chan_spec *)tmp_data;
  1184. ch_sp[PAC1934_CH_ENERGY].channel = cnt + 1;
  1185. ch_sp[PAC1934_CH_ENERGY].scan_index = cnt;
  1186. ch_sp[PAC1934_CH_ENERGY].address = cnt + PAC1934_VPOWER_ACC_1_ADDR;
  1187. ch_sp[PAC1934_CH_POWER].channel = cnt + 1;
  1188. ch_sp[PAC1934_CH_POWER].scan_index = cnt;
  1189. ch_sp[PAC1934_CH_POWER].address = cnt + PAC1934_VPOWER_1_ADDR;
  1190. ch_sp[PAC1934_CH_VOLTAGE].channel = cnt + 1;
  1191. ch_sp[PAC1934_CH_VOLTAGE].scan_index = cnt;
  1192. ch_sp[PAC1934_CH_VOLTAGE].address = cnt + PAC1934_VBUS_1_ADDR;
  1193. ch_sp[PAC1934_CH_CURRENT].channel = cnt + 1;
  1194. ch_sp[PAC1934_CH_CURRENT].scan_index = cnt;
  1195. ch_sp[PAC1934_CH_CURRENT].address = cnt + PAC1934_VSENSE_1_ADDR;
  1196. /*
  1197. * In order to be able to use labels for PAC1934_CH_VOLTAGE, and
  1198. * PAC1934_CH_VOLTAGE_AVERAGE,respectively PAC1934_CH_CURRENT
  1199. * and PAC1934_CH_CURRENT_AVERAGE we need to use different
  1200. * channel numbers. We will add +5 (+1 to maximum PAC channels).
  1201. */
  1202. ch_sp[PAC1934_CH_VOLTAGE_AVERAGE].channel = cnt + 5;
  1203. ch_sp[PAC1934_CH_VOLTAGE_AVERAGE].scan_index = cnt;
  1204. ch_sp[PAC1934_CH_VOLTAGE_AVERAGE].address = cnt + PAC1934_VBUS_AVG_1_ADDR;
  1205. ch_sp[PAC1934_CH_CURRENT_AVERAGE].channel = cnt + 5;
  1206. ch_sp[PAC1934_CH_CURRENT_AVERAGE].scan_index = cnt;
  1207. ch_sp[PAC1934_CH_CURRENT_AVERAGE].address = cnt + PAC1934_VSENSE_AVG_1_ADDR;
  1208. /*
  1209. * now modify the parameters in all channels if the
  1210. * whole chip rail(channel) is bi-directional
  1211. */
  1212. if (info->bi_dir[cnt]) {
  1213. ch_sp[PAC1934_CH_ENERGY].scan_type.sign = 's';
  1214. ch_sp[PAC1934_CH_ENERGY].scan_type.realbits = 47;
  1215. ch_sp[PAC1934_CH_POWER].scan_type.sign = 's';
  1216. ch_sp[PAC1934_CH_POWER].scan_type.realbits = 27;
  1217. ch_sp[PAC1934_CH_VOLTAGE].scan_type.sign = 's';
  1218. ch_sp[PAC1934_CH_VOLTAGE].scan_type.realbits = 15;
  1219. ch_sp[PAC1934_CH_CURRENT].scan_type.sign = 's';
  1220. ch_sp[PAC1934_CH_CURRENT].scan_type.realbits = 15;
  1221. ch_sp[PAC1934_CH_VOLTAGE_AVERAGE].scan_type.sign = 's';
  1222. ch_sp[PAC1934_CH_VOLTAGE_AVERAGE].scan_type.realbits = 15;
  1223. ch_sp[PAC1934_CH_CURRENT_AVERAGE].scan_type.sign = 's';
  1224. ch_sp[PAC1934_CH_CURRENT_AVERAGE].scan_type.realbits = 15;
  1225. }
  1226. tmp_data += sizeof(pac1934_single_channel);
  1227. }
  1228. /*
  1229. * send the updated dynamic channel structure information towards IIO
  1230. * prepare the required field for IIO class registration
  1231. */
  1232. indio_dev->num_channels = attribute_count;
  1233. indio_dev->channels = (const struct iio_chan_spec *)dyn_ch_struct;
  1234. return 0;
  1235. }
  1236. static IIO_DEVICE_ATTR(in_shunt_resistor1, 0644,
  1237. pac1934_shunt_value_show, pac1934_shunt_value_store, 0);
  1238. static IIO_DEVICE_ATTR(in_shunt_resistor2, 0644,
  1239. pac1934_shunt_value_show, pac1934_shunt_value_store, 1);
  1240. static IIO_DEVICE_ATTR(in_shunt_resistor3, 0644,
  1241. pac1934_shunt_value_show, pac1934_shunt_value_store, 2);
  1242. static IIO_DEVICE_ATTR(in_shunt_resistor4, 0644,
  1243. pac1934_shunt_value_show, pac1934_shunt_value_store, 3);
  1244. static int pac1934_prep_custom_attributes(struct pac1934_chip_info *info,
  1245. struct iio_dev *indio_dev)
  1246. {
  1247. int i, active_channels_count = 0;
  1248. struct attribute **pac1934_custom_attr;
  1249. struct attribute_group *pac1934_group;
  1250. struct device *dev = &info->client->dev;
  1251. for (i = 0 ; i < info->phys_channels; i++)
  1252. if (info->active_channels[i])
  1253. active_channels_count++;
  1254. pac1934_group = devm_kzalloc(dev, sizeof(*pac1934_group), GFP_KERNEL);
  1255. if (!pac1934_group)
  1256. return -ENOMEM;
  1257. pac1934_custom_attr = devm_kzalloc(dev,
  1258. (PAC1934_CUSTOM_ATTR_FOR_CHANNEL *
  1259. active_channels_count)
  1260. * sizeof(*pac1934_group) + 1,
  1261. GFP_KERNEL);
  1262. if (!pac1934_custom_attr)
  1263. return -ENOMEM;
  1264. i = 0;
  1265. if (info->active_channels[0])
  1266. pac1934_custom_attr[i++] = PAC1934_DEV_ATTR(in_shunt_resistor1);
  1267. if (info->active_channels[1])
  1268. pac1934_custom_attr[i++] = PAC1934_DEV_ATTR(in_shunt_resistor2);
  1269. if (info->active_channels[2])
  1270. pac1934_custom_attr[i++] = PAC1934_DEV_ATTR(in_shunt_resistor3);
  1271. if (info->active_channels[3])
  1272. pac1934_custom_attr[i] = PAC1934_DEV_ATTR(in_shunt_resistor4);
  1273. pac1934_group->attrs = pac1934_custom_attr;
  1274. info->iio_info.attrs = pac1934_group;
  1275. return 0;
  1276. }
  1277. static const struct iio_info pac1934_info = {
  1278. .read_raw = pac1934_read_raw,
  1279. .write_raw = pac1934_write_raw,
  1280. .read_avail = pac1934_read_avail,
  1281. .read_label = pac1934_read_label,
  1282. };
  1283. static int pac1934_probe(struct i2c_client *client)
  1284. {
  1285. struct pac1934_chip_info *info;
  1286. const struct pac1934_features *chip;
  1287. struct iio_dev *indio_dev;
  1288. int cnt, ret;
  1289. struct device *dev = &client->dev;
  1290. indio_dev = devm_iio_device_alloc(dev, sizeof(*info));
  1291. if (!indio_dev)
  1292. return -ENOMEM;
  1293. info = iio_priv(indio_dev);
  1294. info->client = client;
  1295. /* always start with energy accumulation enabled */
  1296. for (cnt = 0; cnt < PAC1934_MAX_NUM_CHANNELS; cnt++)
  1297. info->enable_energy[cnt] = true;
  1298. ret = pac1934_chip_identify(info);
  1299. if (ret < 0) {
  1300. /*
  1301. * If failed to identify the hardware based on internal
  1302. * registers, try using fallback compatible in device tree
  1303. * to deal with some newer part number.
  1304. */
  1305. chip = i2c_get_match_data(client);
  1306. if (!chip)
  1307. return -EINVAL;
  1308. info->phys_channels = chip->phys_channels;
  1309. indio_dev->name = chip->name;
  1310. } else {
  1311. info->phys_channels = pac1934_chip_config[ret].phys_channels;
  1312. indio_dev->name = pac1934_chip_config[ret].name;
  1313. }
  1314. if (is_acpi_device_node(dev_fwnode(dev)))
  1315. ret = pac1934_acpi_parse_channel_config(client, info);
  1316. else
  1317. /*
  1318. * This makes it possible to use also ACPI PRP0001 for
  1319. * registering the device using device tree properties.
  1320. */
  1321. ret = pac1934_fw_parse_channel_config(client, info);
  1322. if (ret)
  1323. return dev_err_probe(dev, ret,
  1324. "parameter parsing returned an error\n");
  1325. ret = devm_mutex_init(dev, &info->lock);
  1326. if (ret < 0)
  1327. return ret;
  1328. /*
  1329. * do now any chip specific initialization (e.g. read/write
  1330. * some registers), enable/disable certain channels, change the sampling
  1331. * rate to the requested value
  1332. */
  1333. ret = pac1934_chip_configure(info);
  1334. if (ret < 0)
  1335. return ret;
  1336. /* prepare the channel information */
  1337. ret = pac1934_prep_iio_channels(info, indio_dev);
  1338. if (ret < 0)
  1339. return ret;
  1340. info->iio_info = pac1934_info;
  1341. indio_dev->info = &info->iio_info;
  1342. indio_dev->modes = INDIO_DIRECT_MODE;
  1343. ret = pac1934_prep_custom_attributes(info, indio_dev);
  1344. if (ret < 0)
  1345. return dev_err_probe(dev, ret,
  1346. "Can't configure custom attributes for PAC1934 device\n");
  1347. /*
  1348. * read whatever has been accumulated in the chip so far
  1349. * and reset the accumulators
  1350. */
  1351. ret = pac1934_reg_snapshot(info, true, PAC1934_REFRESH_REG_ADDR,
  1352. PAC1934_MIN_UPDATE_WAIT_TIME_US);
  1353. if (ret < 0)
  1354. return ret;
  1355. ret = devm_iio_device_register(dev, indio_dev);
  1356. if (ret < 0)
  1357. return dev_err_probe(dev, ret,
  1358. "Can't register IIO device\n");
  1359. return 0;
  1360. }
  1361. static const struct i2c_device_id pac1934_id[] = {
  1362. { .name = "pac1931", .driver_data = (kernel_ulong_t)&pac1934_chip_config[PAC1931] },
  1363. { .name = "pac1932", .driver_data = (kernel_ulong_t)&pac1934_chip_config[PAC1932] },
  1364. { .name = "pac1933", .driver_data = (kernel_ulong_t)&pac1934_chip_config[PAC1933] },
  1365. { .name = "pac1934", .driver_data = (kernel_ulong_t)&pac1934_chip_config[PAC1934] },
  1366. { }
  1367. };
  1368. MODULE_DEVICE_TABLE(i2c, pac1934_id);
  1369. static const struct of_device_id pac1934_of_match[] = {
  1370. {
  1371. .compatible = "microchip,pac1931",
  1372. .data = &pac1934_chip_config[PAC1931]
  1373. },
  1374. {
  1375. .compatible = "microchip,pac1932",
  1376. .data = &pac1934_chip_config[PAC1932]
  1377. },
  1378. {
  1379. .compatible = "microchip,pac1933",
  1380. .data = &pac1934_chip_config[PAC1933]
  1381. },
  1382. {
  1383. .compatible = "microchip,pac1934",
  1384. .data = &pac1934_chip_config[PAC1934]
  1385. },
  1386. { }
  1387. };
  1388. MODULE_DEVICE_TABLE(of, pac1934_of_match);
  1389. /*
  1390. * using MCHP1930 to be compatible with BIOS ACPI. See example:
  1391. * https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ApplicationNotes/ApplicationNotes/PAC1934-Integration-Notes-for-Microsoft-Windows-10-and-Windows-11-Driver-Support-DS00002534.pdf
  1392. */
  1393. static const struct acpi_device_id pac1934_acpi_match[] = {
  1394. { "MCHP1930", .driver_data = (kernel_ulong_t)&pac1934_chip_config[PAC1934] },
  1395. { }
  1396. };
  1397. MODULE_DEVICE_TABLE(acpi, pac1934_acpi_match);
  1398. static struct i2c_driver pac1934_driver = {
  1399. .driver = {
  1400. .name = "pac1934",
  1401. .of_match_table = pac1934_of_match,
  1402. .acpi_match_table = pac1934_acpi_match
  1403. },
  1404. .probe = pac1934_probe,
  1405. .id_table = pac1934_id,
  1406. };
  1407. module_i2c_driver(pac1934_driver);
  1408. MODULE_AUTHOR("Bogdan Bolocan <bogdan.bolocan@microchip.com>");
  1409. MODULE_AUTHOR("Victor Tudose");
  1410. MODULE_AUTHOR("Marius Cristea <marius.cristea@microchip.com>");
  1411. MODULE_DESCRIPTION("IIO driver for PAC1934 Multi-Channel DC Power/Energy Monitor");
  1412. MODULE_LICENSE("GPL");