regmap.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632
  1. /*
  2. * Register map access API
  3. *
  4. * Copyright 2011 Wolfson Microelectronics plc
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/device.h>
  13. #include <linux/slab.h>
  14. #include <linux/export.h>
  15. #include <linux/mutex.h>
  16. #include <linux/err.h>
  17. #include <linux/of.h>
  18. #include <linux/rbtree.h>
  19. #include <linux/sched.h>
  20. #define CREATE_TRACE_POINTS
  21. #include <trace/events/regmap.h>
  22. #include "internal.h"
  23. /*
  24. * Sometimes for failures during very early init the trace
  25. * infrastructure isn't available early enough to be used. For this
  26. * sort of problem defining LOG_DEVICE will add printks for basic
  27. * register I/O on a specific device.
  28. */
  29. #undef LOG_DEVICE
  30. static int _regmap_update_bits(struct regmap *map, unsigned int reg,
  31. unsigned int mask, unsigned int val,
  32. bool *change);
  33. static int _regmap_bus_reg_read(void *context, unsigned int reg,
  34. unsigned int *val);
  35. static int _regmap_bus_read(void *context, unsigned int reg,
  36. unsigned int *val);
  37. static int _regmap_bus_formatted_write(void *context, unsigned int reg,
  38. unsigned int val);
  39. static int _regmap_bus_reg_write(void *context, unsigned int reg,
  40. unsigned int val);
  41. static int _regmap_bus_raw_write(void *context, unsigned int reg,
  42. unsigned int val);
  43. bool regmap_reg_in_ranges(unsigned int reg,
  44. const struct regmap_range *ranges,
  45. unsigned int nranges)
  46. {
  47. const struct regmap_range *r;
  48. int i;
  49. for (i = 0, r = ranges; i < nranges; i++, r++)
  50. if (regmap_reg_in_range(reg, r))
  51. return true;
  52. return false;
  53. }
  54. EXPORT_SYMBOL_GPL(regmap_reg_in_ranges);
  55. bool regmap_check_range_table(struct regmap *map, unsigned int reg,
  56. const struct regmap_access_table *table)
  57. {
  58. /* Check "no ranges" first */
  59. if (regmap_reg_in_ranges(reg, table->no_ranges, table->n_no_ranges))
  60. return false;
  61. /* In case zero "yes ranges" are supplied, any reg is OK */
  62. if (!table->n_yes_ranges)
  63. return true;
  64. return regmap_reg_in_ranges(reg, table->yes_ranges,
  65. table->n_yes_ranges);
  66. }
  67. EXPORT_SYMBOL_GPL(regmap_check_range_table);
  68. bool regmap_writeable(struct regmap *map, unsigned int reg)
  69. {
  70. if (map->max_register && reg > map->max_register)
  71. return false;
  72. if (map->writeable_reg)
  73. return map->writeable_reg(map->dev, reg);
  74. if (map->wr_table)
  75. return regmap_check_range_table(map, reg, map->wr_table);
  76. return true;
  77. }
  78. bool regmap_readable(struct regmap *map, unsigned int reg)
  79. {
  80. if (map->max_register && reg > map->max_register)
  81. return false;
  82. if (map->format.format_write)
  83. return false;
  84. if (map->readable_reg)
  85. return map->readable_reg(map->dev, reg);
  86. if (map->rd_table)
  87. return regmap_check_range_table(map, reg, map->rd_table);
  88. return true;
  89. }
  90. bool regmap_volatile(struct regmap *map, unsigned int reg)
  91. {
  92. if (!map->format.format_write && !regmap_readable(map, reg))
  93. return false;
  94. if (map->volatile_reg)
  95. return map->volatile_reg(map->dev, reg);
  96. if (map->volatile_table)
  97. return regmap_check_range_table(map, reg, map->volatile_table);
  98. if (map->cache_ops)
  99. return false;
  100. else
  101. return true;
  102. }
  103. bool regmap_precious(struct regmap *map, unsigned int reg)
  104. {
  105. if (!regmap_readable(map, reg))
  106. return false;
  107. if (map->precious_reg)
  108. return map->precious_reg(map->dev, reg);
  109. if (map->precious_table)
  110. return regmap_check_range_table(map, reg, map->precious_table);
  111. return false;
  112. }
  113. static bool regmap_volatile_range(struct regmap *map, unsigned int reg,
  114. size_t num)
  115. {
  116. unsigned int i;
  117. for (i = 0; i < num; i++)
  118. if (!regmap_volatile(map, reg + i))
  119. return false;
  120. return true;
  121. }
  122. static void regmap_format_2_6_write(struct regmap *map,
  123. unsigned int reg, unsigned int val)
  124. {
  125. u8 *out = map->work_buf;
  126. *out = (reg << 6) | val;
  127. }
  128. static void regmap_format_4_12_write(struct regmap *map,
  129. unsigned int reg, unsigned int val)
  130. {
  131. __be16 *out = map->work_buf;
  132. *out = cpu_to_be16((reg << 12) | val);
  133. }
  134. static void regmap_format_7_9_write(struct regmap *map,
  135. unsigned int reg, unsigned int val)
  136. {
  137. __be16 *out = map->work_buf;
  138. *out = cpu_to_be16((reg << 9) | val);
  139. }
  140. static void regmap_format_10_14_write(struct regmap *map,
  141. unsigned int reg, unsigned int val)
  142. {
  143. u8 *out = map->work_buf;
  144. out[2] = val;
  145. out[1] = (val >> 8) | (reg << 6);
  146. out[0] = reg >> 2;
  147. }
  148. static void regmap_format_8(void *buf, unsigned int val, unsigned int shift)
  149. {
  150. u8 *b = buf;
  151. b[0] = val << shift;
  152. }
  153. static void regmap_format_16_be(void *buf, unsigned int val, unsigned int shift)
  154. {
  155. __be16 *b = buf;
  156. b[0] = cpu_to_be16(val << shift);
  157. }
  158. static void regmap_format_16_le(void *buf, unsigned int val, unsigned int shift)
  159. {
  160. __le16 *b = buf;
  161. b[0] = cpu_to_le16(val << shift);
  162. }
  163. static void regmap_format_16_native(void *buf, unsigned int val,
  164. unsigned int shift)
  165. {
  166. *(u16 *)buf = val << shift;
  167. }
  168. static void regmap_format_24(void *buf, unsigned int val, unsigned int shift)
  169. {
  170. u8 *b = buf;
  171. val <<= shift;
  172. b[0] = val >> 16;
  173. b[1] = val >> 8;
  174. b[2] = val;
  175. }
  176. static void regmap_format_32_be(void *buf, unsigned int val, unsigned int shift)
  177. {
  178. __be32 *b = buf;
  179. b[0] = cpu_to_be32(val << shift);
  180. }
  181. static void regmap_format_32_le(void *buf, unsigned int val, unsigned int shift)
  182. {
  183. __le32 *b = buf;
  184. b[0] = cpu_to_le32(val << shift);
  185. }
  186. static void regmap_format_32_native(void *buf, unsigned int val,
  187. unsigned int shift)
  188. {
  189. *(u32 *)buf = val << shift;
  190. }
  191. static void regmap_parse_inplace_noop(void *buf)
  192. {
  193. }
  194. static unsigned int regmap_parse_8(const void *buf)
  195. {
  196. const u8 *b = buf;
  197. return b[0];
  198. }
  199. static unsigned int regmap_parse_16_be(const void *buf)
  200. {
  201. const __be16 *b = buf;
  202. return be16_to_cpu(b[0]);
  203. }
  204. static unsigned int regmap_parse_16_le(const void *buf)
  205. {
  206. const __le16 *b = buf;
  207. return le16_to_cpu(b[0]);
  208. }
  209. static void regmap_parse_16_be_inplace(void *buf)
  210. {
  211. __be16 *b = buf;
  212. b[0] = be16_to_cpu(b[0]);
  213. }
  214. static void regmap_parse_16_le_inplace(void *buf)
  215. {
  216. __le16 *b = buf;
  217. b[0] = le16_to_cpu(b[0]);
  218. }
  219. static unsigned int regmap_parse_16_native(const void *buf)
  220. {
  221. return *(u16 *)buf;
  222. }
  223. static unsigned int regmap_parse_24(const void *buf)
  224. {
  225. const u8 *b = buf;
  226. unsigned int ret = b[2];
  227. ret |= ((unsigned int)b[1]) << 8;
  228. ret |= ((unsigned int)b[0]) << 16;
  229. return ret;
  230. }
  231. static unsigned int regmap_parse_32_be(const void *buf)
  232. {
  233. const __be32 *b = buf;
  234. return be32_to_cpu(b[0]);
  235. }
  236. static unsigned int regmap_parse_32_le(const void *buf)
  237. {
  238. const __le32 *b = buf;
  239. return le32_to_cpu(b[0]);
  240. }
  241. static void regmap_parse_32_be_inplace(void *buf)
  242. {
  243. __be32 *b = buf;
  244. b[0] = be32_to_cpu(b[0]);
  245. }
  246. static void regmap_parse_32_le_inplace(void *buf)
  247. {
  248. __le32 *b = buf;
  249. b[0] = le32_to_cpu(b[0]);
  250. }
  251. static unsigned int regmap_parse_32_native(const void *buf)
  252. {
  253. return *(u32 *)buf;
  254. }
  255. static void regmap_lock_mutex(void *__map)
  256. {
  257. struct regmap *map = __map;
  258. mutex_lock(&map->mutex);
  259. }
  260. static void regmap_unlock_mutex(void *__map)
  261. {
  262. struct regmap *map = __map;
  263. mutex_unlock(&map->mutex);
  264. }
  265. static void regmap_lock_spinlock(void *__map)
  266. __acquires(&map->spinlock)
  267. {
  268. struct regmap *map = __map;
  269. unsigned long flags;
  270. spin_lock_irqsave(&map->spinlock, flags);
  271. map->spinlock_flags = flags;
  272. }
  273. static void regmap_unlock_spinlock(void *__map)
  274. __releases(&map->spinlock)
  275. {
  276. struct regmap *map = __map;
  277. spin_unlock_irqrestore(&map->spinlock, map->spinlock_flags);
  278. }
  279. static void dev_get_regmap_release(struct device *dev, void *res)
  280. {
  281. /*
  282. * We don't actually have anything to do here; the goal here
  283. * is not to manage the regmap but to provide a simple way to
  284. * get the regmap back given a struct device.
  285. */
  286. }
  287. static bool _regmap_range_add(struct regmap *map,
  288. struct regmap_range_node *data)
  289. {
  290. struct rb_root *root = &map->range_tree;
  291. struct rb_node **new = &(root->rb_node), *parent = NULL;
  292. while (*new) {
  293. struct regmap_range_node *this =
  294. container_of(*new, struct regmap_range_node, node);
  295. parent = *new;
  296. if (data->range_max < this->range_min)
  297. new = &((*new)->rb_left);
  298. else if (data->range_min > this->range_max)
  299. new = &((*new)->rb_right);
  300. else
  301. return false;
  302. }
  303. rb_link_node(&data->node, parent, new);
  304. rb_insert_color(&data->node, root);
  305. return true;
  306. }
  307. static struct regmap_range_node *_regmap_range_lookup(struct regmap *map,
  308. unsigned int reg)
  309. {
  310. struct rb_node *node = map->range_tree.rb_node;
  311. while (node) {
  312. struct regmap_range_node *this =
  313. container_of(node, struct regmap_range_node, node);
  314. if (reg < this->range_min)
  315. node = node->rb_left;
  316. else if (reg > this->range_max)
  317. node = node->rb_right;
  318. else
  319. return this;
  320. }
  321. return NULL;
  322. }
  323. static void regmap_range_exit(struct regmap *map)
  324. {
  325. struct rb_node *next;
  326. struct regmap_range_node *range_node;
  327. next = rb_first(&map->range_tree);
  328. while (next) {
  329. range_node = rb_entry(next, struct regmap_range_node, node);
  330. next = rb_next(&range_node->node);
  331. rb_erase(&range_node->node, &map->range_tree);
  332. kfree(range_node);
  333. }
  334. kfree(map->selector_work_buf);
  335. }
  336. int regmap_attach_dev(struct device *dev, struct regmap *map,
  337. const struct regmap_config *config)
  338. {
  339. struct regmap **m;
  340. map->dev = dev;
  341. regmap_debugfs_init(map, config->name);
  342. /* Add a devres resource for dev_get_regmap() */
  343. m = devres_alloc(dev_get_regmap_release, sizeof(*m), GFP_KERNEL);
  344. if (!m) {
  345. regmap_debugfs_exit(map);
  346. return -ENOMEM;
  347. }
  348. *m = map;
  349. devres_add(dev, m);
  350. return 0;
  351. }
  352. EXPORT_SYMBOL_GPL(regmap_attach_dev);
  353. static enum regmap_endian regmap_get_reg_endian(const struct regmap_bus *bus,
  354. const struct regmap_config *config)
  355. {
  356. enum regmap_endian endian;
  357. /* Retrieve the endianness specification from the regmap config */
  358. endian = config->reg_format_endian;
  359. /* If the regmap config specified a non-default value, use that */
  360. if (endian != REGMAP_ENDIAN_DEFAULT)
  361. return endian;
  362. /* Retrieve the endianness specification from the bus config */
  363. if (bus && bus->reg_format_endian_default)
  364. endian = bus->reg_format_endian_default;
  365. /* If the bus specified a non-default value, use that */
  366. if (endian != REGMAP_ENDIAN_DEFAULT)
  367. return endian;
  368. /* Use this if no other value was found */
  369. return REGMAP_ENDIAN_BIG;
  370. }
  371. static enum regmap_endian regmap_get_val_endian(struct device *dev,
  372. const struct regmap_bus *bus,
  373. const struct regmap_config *config)
  374. {
  375. struct device_node *np;
  376. enum regmap_endian endian;
  377. /* Retrieve the endianness specification from the regmap config */
  378. endian = config->val_format_endian;
  379. /* If the regmap config specified a non-default value, use that */
  380. if (endian != REGMAP_ENDIAN_DEFAULT)
  381. return endian;
  382. /* If the dev and dev->of_node exist try to get endianness from DT */
  383. if (dev && dev->of_node) {
  384. np = dev->of_node;
  385. /* Parse the device's DT node for an endianness specification */
  386. if (of_property_read_bool(np, "big-endian"))
  387. endian = REGMAP_ENDIAN_BIG;
  388. else if (of_property_read_bool(np, "little-endian"))
  389. endian = REGMAP_ENDIAN_LITTLE;
  390. /* If the endianness was specified in DT, use that */
  391. if (endian != REGMAP_ENDIAN_DEFAULT)
  392. return endian;
  393. }
  394. /* Retrieve the endianness specification from the bus config */
  395. if (bus && bus->val_format_endian_default)
  396. endian = bus->val_format_endian_default;
  397. /* If the bus specified a non-default value, use that */
  398. if (endian != REGMAP_ENDIAN_DEFAULT)
  399. return endian;
  400. /* Use this if no other value was found */
  401. return REGMAP_ENDIAN_BIG;
  402. }
  403. /**
  404. * regmap_init(): Initialise register map
  405. *
  406. * @dev: Device that will be interacted with
  407. * @bus: Bus-specific callbacks to use with device
  408. * @bus_context: Data passed to bus-specific callbacks
  409. * @config: Configuration for register map
  410. *
  411. * The return value will be an ERR_PTR() on error or a valid pointer to
  412. * a struct regmap. This function should generally not be called
  413. * directly, it should be called by bus-specific init functions.
  414. */
  415. struct regmap *regmap_init(struct device *dev,
  416. const struct regmap_bus *bus,
  417. void *bus_context,
  418. const struct regmap_config *config)
  419. {
  420. struct regmap *map;
  421. int ret = -EINVAL;
  422. enum regmap_endian reg_endian, val_endian;
  423. int i, j;
  424. if (!config)
  425. goto err;
  426. map = kzalloc(sizeof(*map), GFP_KERNEL);
  427. if (map == NULL) {
  428. ret = -ENOMEM;
  429. goto err;
  430. }
  431. if (config->lock && config->unlock) {
  432. map->lock = config->lock;
  433. map->unlock = config->unlock;
  434. map->lock_arg = config->lock_arg;
  435. } else {
  436. if ((bus && bus->fast_io) ||
  437. config->fast_io) {
  438. spin_lock_init(&map->spinlock);
  439. map->lock = regmap_lock_spinlock;
  440. map->unlock = regmap_unlock_spinlock;
  441. } else {
  442. mutex_init(&map->mutex);
  443. map->lock = regmap_lock_mutex;
  444. map->unlock = regmap_unlock_mutex;
  445. }
  446. map->lock_arg = map;
  447. }
  448. map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
  449. map->format.pad_bytes = config->pad_bits / 8;
  450. map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
  451. map->format.buf_size = DIV_ROUND_UP(config->reg_bits +
  452. config->val_bits + config->pad_bits, 8);
  453. map->reg_shift = config->pad_bits % 8;
  454. if (config->reg_stride)
  455. map->reg_stride = config->reg_stride;
  456. else
  457. map->reg_stride = 1;
  458. map->use_single_rw = config->use_single_rw;
  459. map->can_multi_write = config->can_multi_write;
  460. map->dev = dev;
  461. map->bus = bus;
  462. map->bus_context = bus_context;
  463. map->max_register = config->max_register;
  464. map->wr_table = config->wr_table;
  465. map->rd_table = config->rd_table;
  466. map->volatile_table = config->volatile_table;
  467. map->precious_table = config->precious_table;
  468. map->writeable_reg = config->writeable_reg;
  469. map->readable_reg = config->readable_reg;
  470. map->volatile_reg = config->volatile_reg;
  471. map->precious_reg = config->precious_reg;
  472. map->cache_type = config->cache_type;
  473. map->name = config->name;
  474. spin_lock_init(&map->async_lock);
  475. INIT_LIST_HEAD(&map->async_list);
  476. INIT_LIST_HEAD(&map->async_free);
  477. init_waitqueue_head(&map->async_waitq);
  478. if (config->read_flag_mask || config->write_flag_mask) {
  479. map->read_flag_mask = config->read_flag_mask;
  480. map->write_flag_mask = config->write_flag_mask;
  481. } else if (bus) {
  482. map->read_flag_mask = bus->read_flag_mask;
  483. }
  484. if (!bus) {
  485. map->reg_read = config->reg_read;
  486. map->reg_write = config->reg_write;
  487. map->defer_caching = false;
  488. goto skip_format_initialization;
  489. } else if (!bus->read || !bus->write) {
  490. map->reg_read = _regmap_bus_reg_read;
  491. map->reg_write = _regmap_bus_reg_write;
  492. map->defer_caching = false;
  493. goto skip_format_initialization;
  494. } else {
  495. map->reg_read = _regmap_bus_read;
  496. }
  497. reg_endian = regmap_get_reg_endian(bus, config);
  498. val_endian = regmap_get_val_endian(dev, bus, config);
  499. switch (config->reg_bits + map->reg_shift) {
  500. case 2:
  501. switch (config->val_bits) {
  502. case 6:
  503. map->format.format_write = regmap_format_2_6_write;
  504. break;
  505. default:
  506. goto err_map;
  507. }
  508. break;
  509. case 4:
  510. switch (config->val_bits) {
  511. case 12:
  512. map->format.format_write = regmap_format_4_12_write;
  513. break;
  514. default:
  515. goto err_map;
  516. }
  517. break;
  518. case 7:
  519. switch (config->val_bits) {
  520. case 9:
  521. map->format.format_write = regmap_format_7_9_write;
  522. break;
  523. default:
  524. goto err_map;
  525. }
  526. break;
  527. case 10:
  528. switch (config->val_bits) {
  529. case 14:
  530. map->format.format_write = regmap_format_10_14_write;
  531. break;
  532. default:
  533. goto err_map;
  534. }
  535. break;
  536. case 8:
  537. map->format.format_reg = regmap_format_8;
  538. break;
  539. case 16:
  540. switch (reg_endian) {
  541. case REGMAP_ENDIAN_BIG:
  542. map->format.format_reg = regmap_format_16_be;
  543. break;
  544. case REGMAP_ENDIAN_NATIVE:
  545. map->format.format_reg = regmap_format_16_native;
  546. break;
  547. default:
  548. goto err_map;
  549. }
  550. break;
  551. case 24:
  552. if (reg_endian != REGMAP_ENDIAN_BIG)
  553. goto err_map;
  554. map->format.format_reg = regmap_format_24;
  555. break;
  556. case 32:
  557. switch (reg_endian) {
  558. case REGMAP_ENDIAN_BIG:
  559. map->format.format_reg = regmap_format_32_be;
  560. break;
  561. case REGMAP_ENDIAN_NATIVE:
  562. map->format.format_reg = regmap_format_32_native;
  563. break;
  564. default:
  565. goto err_map;
  566. }
  567. break;
  568. default:
  569. goto err_map;
  570. }
  571. if (val_endian == REGMAP_ENDIAN_NATIVE)
  572. map->format.parse_inplace = regmap_parse_inplace_noop;
  573. switch (config->val_bits) {
  574. case 8:
  575. map->format.format_val = regmap_format_8;
  576. map->format.parse_val = regmap_parse_8;
  577. map->format.parse_inplace = regmap_parse_inplace_noop;
  578. break;
  579. case 16:
  580. switch (val_endian) {
  581. case REGMAP_ENDIAN_BIG:
  582. map->format.format_val = regmap_format_16_be;
  583. map->format.parse_val = regmap_parse_16_be;
  584. map->format.parse_inplace = regmap_parse_16_be_inplace;
  585. break;
  586. case REGMAP_ENDIAN_LITTLE:
  587. map->format.format_val = regmap_format_16_le;
  588. map->format.parse_val = regmap_parse_16_le;
  589. map->format.parse_inplace = regmap_parse_16_le_inplace;
  590. break;
  591. case REGMAP_ENDIAN_NATIVE:
  592. map->format.format_val = regmap_format_16_native;
  593. map->format.parse_val = regmap_parse_16_native;
  594. break;
  595. default:
  596. goto err_map;
  597. }
  598. break;
  599. case 24:
  600. if (val_endian != REGMAP_ENDIAN_BIG)
  601. goto err_map;
  602. map->format.format_val = regmap_format_24;
  603. map->format.parse_val = regmap_parse_24;
  604. break;
  605. case 32:
  606. switch (val_endian) {
  607. case REGMAP_ENDIAN_BIG:
  608. map->format.format_val = regmap_format_32_be;
  609. map->format.parse_val = regmap_parse_32_be;
  610. map->format.parse_inplace = regmap_parse_32_be_inplace;
  611. break;
  612. case REGMAP_ENDIAN_LITTLE:
  613. map->format.format_val = regmap_format_32_le;
  614. map->format.parse_val = regmap_parse_32_le;
  615. map->format.parse_inplace = regmap_parse_32_le_inplace;
  616. break;
  617. case REGMAP_ENDIAN_NATIVE:
  618. map->format.format_val = regmap_format_32_native;
  619. map->format.parse_val = regmap_parse_32_native;
  620. break;
  621. default:
  622. goto err_map;
  623. }
  624. break;
  625. }
  626. if (map->format.format_write) {
  627. if ((reg_endian != REGMAP_ENDIAN_BIG) ||
  628. (val_endian != REGMAP_ENDIAN_BIG))
  629. goto err_map;
  630. map->use_single_rw = true;
  631. }
  632. if (!map->format.format_write &&
  633. !(map->format.format_reg && map->format.format_val))
  634. goto err_map;
  635. map->work_buf = kzalloc(map->format.buf_size, GFP_KERNEL);
  636. if (map->work_buf == NULL) {
  637. ret = -ENOMEM;
  638. goto err_map;
  639. }
  640. if (map->format.format_write) {
  641. map->defer_caching = false;
  642. map->reg_write = _regmap_bus_formatted_write;
  643. } else if (map->format.format_val) {
  644. map->defer_caching = true;
  645. map->reg_write = _regmap_bus_raw_write;
  646. }
  647. skip_format_initialization:
  648. map->range_tree = RB_ROOT;
  649. for (i = 0; i < config->num_ranges; i++) {
  650. const struct regmap_range_cfg *range_cfg = &config->ranges[i];
  651. struct regmap_range_node *new;
  652. /* Sanity check */
  653. if (range_cfg->range_max < range_cfg->range_min) {
  654. dev_err(map->dev, "Invalid range %d: %d < %d\n", i,
  655. range_cfg->range_max, range_cfg->range_min);
  656. goto err_range;
  657. }
  658. if (range_cfg->range_max > map->max_register) {
  659. dev_err(map->dev, "Invalid range %d: %d > %d\n", i,
  660. range_cfg->range_max, map->max_register);
  661. goto err_range;
  662. }
  663. if (range_cfg->selector_reg > map->max_register) {
  664. dev_err(map->dev,
  665. "Invalid range %d: selector out of map\n", i);
  666. goto err_range;
  667. }
  668. if (range_cfg->window_len == 0) {
  669. dev_err(map->dev, "Invalid range %d: window_len 0\n",
  670. i);
  671. goto err_range;
  672. }
  673. /* Make sure, that this register range has no selector
  674. or data window within its boundary */
  675. for (j = 0; j < config->num_ranges; j++) {
  676. unsigned sel_reg = config->ranges[j].selector_reg;
  677. unsigned win_min = config->ranges[j].window_start;
  678. unsigned win_max = win_min +
  679. config->ranges[j].window_len - 1;
  680. /* Allow data window inside its own virtual range */
  681. if (j == i)
  682. continue;
  683. if (range_cfg->range_min <= sel_reg &&
  684. sel_reg <= range_cfg->range_max) {
  685. dev_err(map->dev,
  686. "Range %d: selector for %d in window\n",
  687. i, j);
  688. goto err_range;
  689. }
  690. if (!(win_max < range_cfg->range_min ||
  691. win_min > range_cfg->range_max)) {
  692. dev_err(map->dev,
  693. "Range %d: window for %d in window\n",
  694. i, j);
  695. goto err_range;
  696. }
  697. }
  698. new = kzalloc(sizeof(*new), GFP_KERNEL);
  699. if (new == NULL) {
  700. ret = -ENOMEM;
  701. goto err_range;
  702. }
  703. new->map = map;
  704. new->name = range_cfg->name;
  705. new->range_min = range_cfg->range_min;
  706. new->range_max = range_cfg->range_max;
  707. new->selector_reg = range_cfg->selector_reg;
  708. new->selector_mask = range_cfg->selector_mask;
  709. new->selector_shift = range_cfg->selector_shift;
  710. new->window_start = range_cfg->window_start;
  711. new->window_len = range_cfg->window_len;
  712. if (!_regmap_range_add(map, new)) {
  713. dev_err(map->dev, "Failed to add range %d\n", i);
  714. kfree(new);
  715. goto err_range;
  716. }
  717. if (map->selector_work_buf == NULL) {
  718. map->selector_work_buf =
  719. kzalloc(map->format.buf_size, GFP_KERNEL);
  720. if (map->selector_work_buf == NULL) {
  721. ret = -ENOMEM;
  722. goto err_range;
  723. }
  724. }
  725. }
  726. ret = regcache_init(map, config);
  727. if (ret != 0)
  728. goto err_range;
  729. if (dev) {
  730. ret = regmap_attach_dev(dev, map, config);
  731. if (ret != 0)
  732. goto err_regcache;
  733. }
  734. return map;
  735. err_regcache:
  736. regcache_exit(map);
  737. err_range:
  738. regmap_range_exit(map);
  739. kfree(map->work_buf);
  740. err_map:
  741. kfree(map);
  742. err:
  743. return ERR_PTR(ret);
  744. }
  745. EXPORT_SYMBOL_GPL(regmap_init);
  746. static void devm_regmap_release(struct device *dev, void *res)
  747. {
  748. regmap_exit(*(struct regmap **)res);
  749. }
  750. /**
  751. * devm_regmap_init(): Initialise managed register map
  752. *
  753. * @dev: Device that will be interacted with
  754. * @bus: Bus-specific callbacks to use with device
  755. * @bus_context: Data passed to bus-specific callbacks
  756. * @config: Configuration for register map
  757. *
  758. * The return value will be an ERR_PTR() on error or a valid pointer
  759. * to a struct regmap. This function should generally not be called
  760. * directly, it should be called by bus-specific init functions. The
  761. * map will be automatically freed by the device management code.
  762. */
  763. struct regmap *devm_regmap_init(struct device *dev,
  764. const struct regmap_bus *bus,
  765. void *bus_context,
  766. const struct regmap_config *config)
  767. {
  768. struct regmap **ptr, *regmap;
  769. ptr = devres_alloc(devm_regmap_release, sizeof(*ptr), GFP_KERNEL);
  770. if (!ptr)
  771. return ERR_PTR(-ENOMEM);
  772. regmap = regmap_init(dev, bus, bus_context, config);
  773. if (!IS_ERR(regmap)) {
  774. *ptr = regmap;
  775. devres_add(dev, ptr);
  776. } else {
  777. devres_free(ptr);
  778. }
  779. return regmap;
  780. }
  781. EXPORT_SYMBOL_GPL(devm_regmap_init);
  782. static void regmap_field_init(struct regmap_field *rm_field,
  783. struct regmap *regmap, struct reg_field reg_field)
  784. {
  785. rm_field->regmap = regmap;
  786. rm_field->reg = reg_field.reg;
  787. rm_field->shift = reg_field.lsb;
  788. rm_field->mask = GENMASK(reg_field.msb, reg_field.lsb);
  789. rm_field->id_size = reg_field.id_size;
  790. rm_field->id_offset = reg_field.id_offset;
  791. }
  792. /**
  793. * devm_regmap_field_alloc(): Allocate and initialise a register field
  794. * in a register map.
  795. *
  796. * @dev: Device that will be interacted with
  797. * @regmap: regmap bank in which this register field is located.
  798. * @reg_field: Register field with in the bank.
  799. *
  800. * The return value will be an ERR_PTR() on error or a valid pointer
  801. * to a struct regmap_field. The regmap_field will be automatically freed
  802. * by the device management code.
  803. */
  804. struct regmap_field *devm_regmap_field_alloc(struct device *dev,
  805. struct regmap *regmap, struct reg_field reg_field)
  806. {
  807. struct regmap_field *rm_field = devm_kzalloc(dev,
  808. sizeof(*rm_field), GFP_KERNEL);
  809. if (!rm_field)
  810. return ERR_PTR(-ENOMEM);
  811. regmap_field_init(rm_field, regmap, reg_field);
  812. return rm_field;
  813. }
  814. EXPORT_SYMBOL_GPL(devm_regmap_field_alloc);
  815. /**
  816. * devm_regmap_field_free(): Free register field allocated using
  817. * devm_regmap_field_alloc. Usally drivers need not call this function,
  818. * as the memory allocated via devm will be freed as per device-driver
  819. * life-cyle.
  820. *
  821. * @dev: Device that will be interacted with
  822. * @field: regmap field which should be freed.
  823. */
  824. void devm_regmap_field_free(struct device *dev,
  825. struct regmap_field *field)
  826. {
  827. devm_kfree(dev, field);
  828. }
  829. EXPORT_SYMBOL_GPL(devm_regmap_field_free);
  830. /**
  831. * regmap_field_alloc(): Allocate and initialise a register field
  832. * in a register map.
  833. *
  834. * @regmap: regmap bank in which this register field is located.
  835. * @reg_field: Register field with in the bank.
  836. *
  837. * The return value will be an ERR_PTR() on error or a valid pointer
  838. * to a struct regmap_field. The regmap_field should be freed by the
  839. * user once its finished working with it using regmap_field_free().
  840. */
  841. struct regmap_field *regmap_field_alloc(struct regmap *regmap,
  842. struct reg_field reg_field)
  843. {
  844. struct regmap_field *rm_field = kzalloc(sizeof(*rm_field), GFP_KERNEL);
  845. if (!rm_field)
  846. return ERR_PTR(-ENOMEM);
  847. regmap_field_init(rm_field, regmap, reg_field);
  848. return rm_field;
  849. }
  850. EXPORT_SYMBOL_GPL(regmap_field_alloc);
  851. /**
  852. * regmap_field_free(): Free register field allocated using regmap_field_alloc
  853. *
  854. * @field: regmap field which should be freed.
  855. */
  856. void regmap_field_free(struct regmap_field *field)
  857. {
  858. kfree(field);
  859. }
  860. EXPORT_SYMBOL_GPL(regmap_field_free);
  861. /**
  862. * regmap_reinit_cache(): Reinitialise the current register cache
  863. *
  864. * @map: Register map to operate on.
  865. * @config: New configuration. Only the cache data will be used.
  866. *
  867. * Discard any existing register cache for the map and initialize a
  868. * new cache. This can be used to restore the cache to defaults or to
  869. * update the cache configuration to reflect runtime discovery of the
  870. * hardware.
  871. *
  872. * No explicit locking is done here, the user needs to ensure that
  873. * this function will not race with other calls to regmap.
  874. */
  875. int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
  876. {
  877. regcache_exit(map);
  878. regmap_debugfs_exit(map);
  879. map->max_register = config->max_register;
  880. map->writeable_reg = config->writeable_reg;
  881. map->readable_reg = config->readable_reg;
  882. map->volatile_reg = config->volatile_reg;
  883. map->precious_reg = config->precious_reg;
  884. map->cache_type = config->cache_type;
  885. regmap_debugfs_init(map, config->name);
  886. map->cache_bypass = false;
  887. map->cache_only = false;
  888. return regcache_init(map, config);
  889. }
  890. EXPORT_SYMBOL_GPL(regmap_reinit_cache);
  891. /**
  892. * regmap_exit(): Free a previously allocated register map
  893. */
  894. void regmap_exit(struct regmap *map)
  895. {
  896. struct regmap_async *async;
  897. regcache_exit(map);
  898. regmap_debugfs_exit(map);
  899. regmap_range_exit(map);
  900. if (map->bus && map->bus->free_context)
  901. map->bus->free_context(map->bus_context);
  902. kfree(map->work_buf);
  903. while (!list_empty(&map->async_free)) {
  904. async = list_first_entry_or_null(&map->async_free,
  905. struct regmap_async,
  906. list);
  907. list_del(&async->list);
  908. kfree(async->work_buf);
  909. kfree(async);
  910. }
  911. kfree(map);
  912. }
  913. EXPORT_SYMBOL_GPL(regmap_exit);
  914. static int dev_get_regmap_match(struct device *dev, void *res, void *data)
  915. {
  916. struct regmap **r = res;
  917. if (!r || !*r) {
  918. WARN_ON(!r || !*r);
  919. return 0;
  920. }
  921. /* If the user didn't specify a name match any */
  922. if (data)
  923. return (*r)->name == data;
  924. else
  925. return 1;
  926. }
  927. /**
  928. * dev_get_regmap(): Obtain the regmap (if any) for a device
  929. *
  930. * @dev: Device to retrieve the map for
  931. * @name: Optional name for the register map, usually NULL.
  932. *
  933. * Returns the regmap for the device if one is present, or NULL. If
  934. * name is specified then it must match the name specified when
  935. * registering the device, if it is NULL then the first regmap found
  936. * will be used. Devices with multiple register maps are very rare,
  937. * generic code should normally not need to specify a name.
  938. */
  939. struct regmap *dev_get_regmap(struct device *dev, const char *name)
  940. {
  941. struct regmap **r = devres_find(dev, dev_get_regmap_release,
  942. dev_get_regmap_match, (void *)name);
  943. if (!r)
  944. return NULL;
  945. return *r;
  946. }
  947. EXPORT_SYMBOL_GPL(dev_get_regmap);
  948. /**
  949. * regmap_get_device(): Obtain the device from a regmap
  950. *
  951. * @map: Register map to operate on.
  952. *
  953. * Returns the underlying device that the regmap has been created for.
  954. */
  955. struct device *regmap_get_device(struct regmap *map)
  956. {
  957. return map->dev;
  958. }
  959. EXPORT_SYMBOL_GPL(regmap_get_device);
  960. static int _regmap_select_page(struct regmap *map, unsigned int *reg,
  961. struct regmap_range_node *range,
  962. unsigned int val_num)
  963. {
  964. void *orig_work_buf;
  965. unsigned int win_offset;
  966. unsigned int win_page;
  967. bool page_chg;
  968. int ret;
  969. win_offset = (*reg - range->range_min) % range->window_len;
  970. win_page = (*reg - range->range_min) / range->window_len;
  971. if (val_num > 1) {
  972. /* Bulk write shouldn't cross range boundary */
  973. if (*reg + val_num - 1 > range->range_max)
  974. return -EINVAL;
  975. /* ... or single page boundary */
  976. if (val_num > range->window_len - win_offset)
  977. return -EINVAL;
  978. }
  979. /* It is possible to have selector register inside data window.
  980. In that case, selector register is located on every page and
  981. it needs no page switching, when accessed alone. */
  982. if (val_num > 1 ||
  983. range->window_start + win_offset != range->selector_reg) {
  984. /* Use separate work_buf during page switching */
  985. orig_work_buf = map->work_buf;
  986. map->work_buf = map->selector_work_buf;
  987. ret = _regmap_update_bits(map, range->selector_reg,
  988. range->selector_mask,
  989. win_page << range->selector_shift,
  990. &page_chg);
  991. map->work_buf = orig_work_buf;
  992. if (ret != 0)
  993. return ret;
  994. }
  995. *reg = range->window_start + win_offset;
  996. return 0;
  997. }
  998. int _regmap_raw_write(struct regmap *map, unsigned int reg,
  999. const void *val, size_t val_len)
  1000. {
  1001. struct regmap_range_node *range;
  1002. unsigned long flags;
  1003. u8 *u8 = map->work_buf;
  1004. void *work_val = map->work_buf + map->format.reg_bytes +
  1005. map->format.pad_bytes;
  1006. void *buf;
  1007. int ret = -ENOTSUPP;
  1008. size_t len;
  1009. int i;
  1010. WARN_ON(!map->bus);
  1011. /* Check for unwritable registers before we start */
  1012. if (map->writeable_reg)
  1013. for (i = 0; i < val_len / map->format.val_bytes; i++)
  1014. if (!map->writeable_reg(map->dev,
  1015. reg + (i * map->reg_stride)))
  1016. return -EINVAL;
  1017. if (!map->cache_bypass && map->format.parse_val) {
  1018. unsigned int ival;
  1019. int val_bytes = map->format.val_bytes;
  1020. for (i = 0; i < val_len / val_bytes; i++) {
  1021. ival = map->format.parse_val(val + (i * val_bytes));
  1022. ret = regcache_write(map, reg + (i * map->reg_stride),
  1023. ival);
  1024. if (ret) {
  1025. dev_err(map->dev,
  1026. "Error in caching of register: %x ret: %d\n",
  1027. reg + i, ret);
  1028. return ret;
  1029. }
  1030. }
  1031. if (map->cache_only) {
  1032. map->cache_dirty = true;
  1033. return 0;
  1034. }
  1035. }
  1036. range = _regmap_range_lookup(map, reg);
  1037. if (range) {
  1038. int val_num = val_len / map->format.val_bytes;
  1039. int win_offset = (reg - range->range_min) % range->window_len;
  1040. int win_residue = range->window_len - win_offset;
  1041. /* If the write goes beyond the end of the window split it */
  1042. while (val_num > win_residue) {
  1043. dev_dbg(map->dev, "Writing window %d/%zu\n",
  1044. win_residue, val_len / map->format.val_bytes);
  1045. ret = _regmap_raw_write(map, reg, val, win_residue *
  1046. map->format.val_bytes);
  1047. if (ret != 0)
  1048. return ret;
  1049. reg += win_residue;
  1050. val_num -= win_residue;
  1051. val += win_residue * map->format.val_bytes;
  1052. val_len -= win_residue * map->format.val_bytes;
  1053. win_offset = (reg - range->range_min) %
  1054. range->window_len;
  1055. win_residue = range->window_len - win_offset;
  1056. }
  1057. ret = _regmap_select_page(map, &reg, range, val_num);
  1058. if (ret != 0)
  1059. return ret;
  1060. }
  1061. map->format.format_reg(map->work_buf, reg, map->reg_shift);
  1062. u8[0] |= map->write_flag_mask;
  1063. /*
  1064. * Essentially all I/O mechanisms will be faster with a single
  1065. * buffer to write. Since register syncs often generate raw
  1066. * writes of single registers optimise that case.
  1067. */
  1068. if (val != work_val && val_len == map->format.val_bytes) {
  1069. memcpy(work_val, val, map->format.val_bytes);
  1070. val = work_val;
  1071. }
  1072. if (map->async && map->bus->async_write) {
  1073. struct regmap_async *async;
  1074. trace_regmap_async_write_start(map, reg, val_len);
  1075. spin_lock_irqsave(&map->async_lock, flags);
  1076. async = list_first_entry_or_null(&map->async_free,
  1077. struct regmap_async,
  1078. list);
  1079. if (async)
  1080. list_del(&async->list);
  1081. spin_unlock_irqrestore(&map->async_lock, flags);
  1082. if (!async) {
  1083. async = map->bus->async_alloc();
  1084. if (!async)
  1085. return -ENOMEM;
  1086. async->work_buf = kzalloc(map->format.buf_size,
  1087. GFP_KERNEL | GFP_DMA);
  1088. if (!async->work_buf) {
  1089. kfree(async);
  1090. return -ENOMEM;
  1091. }
  1092. }
  1093. async->map = map;
  1094. /* If the caller supplied the value we can use it safely. */
  1095. memcpy(async->work_buf, map->work_buf, map->format.pad_bytes +
  1096. map->format.reg_bytes + map->format.val_bytes);
  1097. spin_lock_irqsave(&map->async_lock, flags);
  1098. list_add_tail(&async->list, &map->async_list);
  1099. spin_unlock_irqrestore(&map->async_lock, flags);
  1100. if (val != work_val)
  1101. ret = map->bus->async_write(map->bus_context,
  1102. async->work_buf,
  1103. map->format.reg_bytes +
  1104. map->format.pad_bytes,
  1105. val, val_len, async);
  1106. else
  1107. ret = map->bus->async_write(map->bus_context,
  1108. async->work_buf,
  1109. map->format.reg_bytes +
  1110. map->format.pad_bytes +
  1111. val_len, NULL, 0, async);
  1112. if (ret != 0) {
  1113. dev_err(map->dev, "Failed to schedule write: %d\n",
  1114. ret);
  1115. spin_lock_irqsave(&map->async_lock, flags);
  1116. list_move(&async->list, &map->async_free);
  1117. spin_unlock_irqrestore(&map->async_lock, flags);
  1118. }
  1119. return ret;
  1120. }
  1121. trace_regmap_hw_write_start(map, reg, val_len / map->format.val_bytes);
  1122. /* If we're doing a single register write we can probably just
  1123. * send the work_buf directly, otherwise try to do a gather
  1124. * write.
  1125. */
  1126. if (val == work_val)
  1127. ret = map->bus->write(map->bus_context, map->work_buf,
  1128. map->format.reg_bytes +
  1129. map->format.pad_bytes +
  1130. val_len);
  1131. else if (map->bus->gather_write)
  1132. ret = map->bus->gather_write(map->bus_context, map->work_buf,
  1133. map->format.reg_bytes +
  1134. map->format.pad_bytes,
  1135. val, val_len);
  1136. /* If that didn't work fall back on linearising by hand. */
  1137. if (ret == -ENOTSUPP) {
  1138. len = map->format.reg_bytes + map->format.pad_bytes + val_len;
  1139. buf = kzalloc(len, GFP_KERNEL);
  1140. if (!buf)
  1141. return -ENOMEM;
  1142. memcpy(buf, map->work_buf, map->format.reg_bytes);
  1143. memcpy(buf + map->format.reg_bytes + map->format.pad_bytes,
  1144. val, val_len);
  1145. ret = map->bus->write(map->bus_context, buf, len);
  1146. kfree(buf);
  1147. }
  1148. trace_regmap_hw_write_done(map, reg, val_len / map->format.val_bytes);
  1149. return ret;
  1150. }
  1151. /**
  1152. * regmap_can_raw_write - Test if regmap_raw_write() is supported
  1153. *
  1154. * @map: Map to check.
  1155. */
  1156. bool regmap_can_raw_write(struct regmap *map)
  1157. {
  1158. return map->bus && map->format.format_val && map->format.format_reg;
  1159. }
  1160. EXPORT_SYMBOL_GPL(regmap_can_raw_write);
  1161. static int _regmap_bus_formatted_write(void *context, unsigned int reg,
  1162. unsigned int val)
  1163. {
  1164. int ret;
  1165. struct regmap_range_node *range;
  1166. struct regmap *map = context;
  1167. WARN_ON(!map->bus || !map->format.format_write);
  1168. range = _regmap_range_lookup(map, reg);
  1169. if (range) {
  1170. ret = _regmap_select_page(map, &reg, range, 1);
  1171. if (ret != 0)
  1172. return ret;
  1173. }
  1174. map->format.format_write(map, reg, val);
  1175. trace_regmap_hw_write_start(map, reg, 1);
  1176. ret = map->bus->write(map->bus_context, map->work_buf,
  1177. map->format.buf_size);
  1178. trace_regmap_hw_write_done(map, reg, 1);
  1179. return ret;
  1180. }
  1181. static int _regmap_bus_reg_write(void *context, unsigned int reg,
  1182. unsigned int val)
  1183. {
  1184. struct regmap *map = context;
  1185. return map->bus->reg_write(map->bus_context, reg, val);
  1186. }
  1187. static int _regmap_bus_raw_write(void *context, unsigned int reg,
  1188. unsigned int val)
  1189. {
  1190. struct regmap *map = context;
  1191. WARN_ON(!map->bus || !map->format.format_val);
  1192. map->format.format_val(map->work_buf + map->format.reg_bytes
  1193. + map->format.pad_bytes, val, 0);
  1194. return _regmap_raw_write(map, reg,
  1195. map->work_buf +
  1196. map->format.reg_bytes +
  1197. map->format.pad_bytes,
  1198. map->format.val_bytes);
  1199. }
  1200. static inline void *_regmap_map_get_context(struct regmap *map)
  1201. {
  1202. return (map->bus) ? map : map->bus_context;
  1203. }
  1204. int _regmap_write(struct regmap *map, unsigned int reg,
  1205. unsigned int val)
  1206. {
  1207. int ret;
  1208. void *context = _regmap_map_get_context(map);
  1209. if (!regmap_writeable(map, reg))
  1210. return -EIO;
  1211. if (!map->cache_bypass && !map->defer_caching) {
  1212. ret = regcache_write(map, reg, val);
  1213. if (ret != 0)
  1214. return ret;
  1215. if (map->cache_only) {
  1216. map->cache_dirty = true;
  1217. return 0;
  1218. }
  1219. }
  1220. #ifdef LOG_DEVICE
  1221. if (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
  1222. dev_info(map->dev, "%x <= %x\n", reg, val);
  1223. #endif
  1224. trace_regmap_reg_write(map, reg, val);
  1225. return map->reg_write(context, reg, val);
  1226. }
  1227. /**
  1228. * regmap_write(): Write a value to a single register
  1229. *
  1230. * @map: Register map to write to
  1231. * @reg: Register to write to
  1232. * @val: Value to be written
  1233. *
  1234. * A value of zero will be returned on success, a negative errno will
  1235. * be returned in error cases.
  1236. */
  1237. int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
  1238. {
  1239. int ret;
  1240. if (reg % map->reg_stride)
  1241. return -EINVAL;
  1242. map->lock(map->lock_arg);
  1243. ret = _regmap_write(map, reg, val);
  1244. map->unlock(map->lock_arg);
  1245. return ret;
  1246. }
  1247. EXPORT_SYMBOL_GPL(regmap_write);
  1248. /**
  1249. * regmap_write_async(): Write a value to a single register asynchronously
  1250. *
  1251. * @map: Register map to write to
  1252. * @reg: Register to write to
  1253. * @val: Value to be written
  1254. *
  1255. * A value of zero will be returned on success, a negative errno will
  1256. * be returned in error cases.
  1257. */
  1258. int regmap_write_async(struct regmap *map, unsigned int reg, unsigned int val)
  1259. {
  1260. int ret;
  1261. if (reg % map->reg_stride)
  1262. return -EINVAL;
  1263. map->lock(map->lock_arg);
  1264. map->async = true;
  1265. ret = _regmap_write(map, reg, val);
  1266. map->async = false;
  1267. map->unlock(map->lock_arg);
  1268. return ret;
  1269. }
  1270. EXPORT_SYMBOL_GPL(regmap_write_async);
  1271. /**
  1272. * regmap_raw_write(): Write raw values to one or more registers
  1273. *
  1274. * @map: Register map to write to
  1275. * @reg: Initial register to write to
  1276. * @val: Block of data to be written, laid out for direct transmission to the
  1277. * device
  1278. * @val_len: Length of data pointed to by val.
  1279. *
  1280. * This function is intended to be used for things like firmware
  1281. * download where a large block of data needs to be transferred to the
  1282. * device. No formatting will be done on the data provided.
  1283. *
  1284. * A value of zero will be returned on success, a negative errno will
  1285. * be returned in error cases.
  1286. */
  1287. int regmap_raw_write(struct regmap *map, unsigned int reg,
  1288. const void *val, size_t val_len)
  1289. {
  1290. int ret;
  1291. if (!regmap_can_raw_write(map))
  1292. return -EINVAL;
  1293. if (val_len % map->format.val_bytes)
  1294. return -EINVAL;
  1295. map->lock(map->lock_arg);
  1296. ret = _regmap_raw_write(map, reg, val, val_len);
  1297. map->unlock(map->lock_arg);
  1298. return ret;
  1299. }
  1300. EXPORT_SYMBOL_GPL(regmap_raw_write);
  1301. /**
  1302. * regmap_field_write(): Write a value to a single register field
  1303. *
  1304. * @field: Register field to write to
  1305. * @val: Value to be written
  1306. *
  1307. * A value of zero will be returned on success, a negative errno will
  1308. * be returned in error cases.
  1309. */
  1310. int regmap_field_write(struct regmap_field *field, unsigned int val)
  1311. {
  1312. return regmap_update_bits(field->regmap, field->reg,
  1313. field->mask, val << field->shift);
  1314. }
  1315. EXPORT_SYMBOL_GPL(regmap_field_write);
  1316. /**
  1317. * regmap_field_update_bits(): Perform a read/modify/write cycle
  1318. * on the register field
  1319. *
  1320. * @field: Register field to write to
  1321. * @mask: Bitmask to change
  1322. * @val: Value to be written
  1323. *
  1324. * A value of zero will be returned on success, a negative errno will
  1325. * be returned in error cases.
  1326. */
  1327. int regmap_field_update_bits(struct regmap_field *field, unsigned int mask, unsigned int val)
  1328. {
  1329. mask = (mask << field->shift) & field->mask;
  1330. return regmap_update_bits(field->regmap, field->reg,
  1331. mask, val << field->shift);
  1332. }
  1333. EXPORT_SYMBOL_GPL(regmap_field_update_bits);
  1334. /**
  1335. * regmap_fields_write(): Write a value to a single register field with port ID
  1336. *
  1337. * @field: Register field to write to
  1338. * @id: port ID
  1339. * @val: Value to be written
  1340. *
  1341. * A value of zero will be returned on success, a negative errno will
  1342. * be returned in error cases.
  1343. */
  1344. int regmap_fields_write(struct regmap_field *field, unsigned int id,
  1345. unsigned int val)
  1346. {
  1347. if (id >= field->id_size)
  1348. return -EINVAL;
  1349. return regmap_update_bits(field->regmap,
  1350. field->reg + (field->id_offset * id),
  1351. field->mask, val << field->shift);
  1352. }
  1353. EXPORT_SYMBOL_GPL(regmap_fields_write);
  1354. /**
  1355. * regmap_fields_update_bits(): Perform a read/modify/write cycle
  1356. * on the register field
  1357. *
  1358. * @field: Register field to write to
  1359. * @id: port ID
  1360. * @mask: Bitmask to change
  1361. * @val: Value to be written
  1362. *
  1363. * A value of zero will be returned on success, a negative errno will
  1364. * be returned in error cases.
  1365. */
  1366. int regmap_fields_update_bits(struct regmap_field *field, unsigned int id,
  1367. unsigned int mask, unsigned int val)
  1368. {
  1369. if (id >= field->id_size)
  1370. return -EINVAL;
  1371. mask = (mask << field->shift) & field->mask;
  1372. return regmap_update_bits(field->regmap,
  1373. field->reg + (field->id_offset * id),
  1374. mask, val << field->shift);
  1375. }
  1376. EXPORT_SYMBOL_GPL(regmap_fields_update_bits);
  1377. /*
  1378. * regmap_bulk_write(): Write multiple registers to the device
  1379. *
  1380. * @map: Register map to write to
  1381. * @reg: First register to be write from
  1382. * @val: Block of data to be written, in native register size for device
  1383. * @val_count: Number of registers to write
  1384. *
  1385. * This function is intended to be used for writing a large block of
  1386. * data to the device either in single transfer or multiple transfer.
  1387. *
  1388. * A value of zero will be returned on success, a negative errno will
  1389. * be returned in error cases.
  1390. */
  1391. int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
  1392. size_t val_count)
  1393. {
  1394. int ret = 0, i;
  1395. size_t val_bytes = map->format.val_bytes;
  1396. if (map->bus && !map->format.parse_inplace)
  1397. return -EINVAL;
  1398. if (reg % map->reg_stride)
  1399. return -EINVAL;
  1400. /*
  1401. * Some devices don't support bulk write, for
  1402. * them we have a series of single write operations.
  1403. */
  1404. if (!map->bus || map->use_single_rw) {
  1405. map->lock(map->lock_arg);
  1406. for (i = 0; i < val_count; i++) {
  1407. unsigned int ival;
  1408. switch (val_bytes) {
  1409. case 1:
  1410. ival = *(u8 *)(val + (i * val_bytes));
  1411. break;
  1412. case 2:
  1413. ival = *(u16 *)(val + (i * val_bytes));
  1414. break;
  1415. case 4:
  1416. ival = *(u32 *)(val + (i * val_bytes));
  1417. break;
  1418. #ifdef CONFIG_64BIT
  1419. case 8:
  1420. ival = *(u64 *)(val + (i * val_bytes));
  1421. break;
  1422. #endif
  1423. default:
  1424. ret = -EINVAL;
  1425. goto out;
  1426. }
  1427. ret = _regmap_write(map, reg + (i * map->reg_stride),
  1428. ival);
  1429. if (ret != 0)
  1430. goto out;
  1431. }
  1432. out:
  1433. map->unlock(map->lock_arg);
  1434. } else {
  1435. void *wval;
  1436. if (!val_count)
  1437. return -EINVAL;
  1438. wval = kmemdup(val, val_count * val_bytes, GFP_KERNEL);
  1439. if (!wval) {
  1440. dev_err(map->dev, "Error in memory allocation\n");
  1441. return -ENOMEM;
  1442. }
  1443. for (i = 0; i < val_count * val_bytes; i += val_bytes)
  1444. map->format.parse_inplace(wval + i);
  1445. map->lock(map->lock_arg);
  1446. ret = _regmap_raw_write(map, reg, wval, val_bytes * val_count);
  1447. map->unlock(map->lock_arg);
  1448. kfree(wval);
  1449. }
  1450. return ret;
  1451. }
  1452. EXPORT_SYMBOL_GPL(regmap_bulk_write);
  1453. /*
  1454. * _regmap_raw_multi_reg_write()
  1455. *
  1456. * the (register,newvalue) pairs in regs have not been formatted, but
  1457. * they are all in the same page and have been changed to being page
  1458. * relative. The page register has been written if that was neccessary.
  1459. */
  1460. static int _regmap_raw_multi_reg_write(struct regmap *map,
  1461. const struct reg_default *regs,
  1462. size_t num_regs)
  1463. {
  1464. int ret;
  1465. void *buf;
  1466. int i;
  1467. u8 *u8;
  1468. size_t val_bytes = map->format.val_bytes;
  1469. size_t reg_bytes = map->format.reg_bytes;
  1470. size_t pad_bytes = map->format.pad_bytes;
  1471. size_t pair_size = reg_bytes + pad_bytes + val_bytes;
  1472. size_t len = pair_size * num_regs;
  1473. if (!len)
  1474. return -EINVAL;
  1475. buf = kzalloc(len, GFP_KERNEL);
  1476. if (!buf)
  1477. return -ENOMEM;
  1478. /* We have to linearise by hand. */
  1479. u8 = buf;
  1480. for (i = 0; i < num_regs; i++) {
  1481. int reg = regs[i].reg;
  1482. int val = regs[i].def;
  1483. trace_regmap_hw_write_start(map, reg, 1);
  1484. map->format.format_reg(u8, reg, map->reg_shift);
  1485. u8 += reg_bytes + pad_bytes;
  1486. map->format.format_val(u8, val, 0);
  1487. u8 += val_bytes;
  1488. }
  1489. u8 = buf;
  1490. *u8 |= map->write_flag_mask;
  1491. ret = map->bus->write(map->bus_context, buf, len);
  1492. kfree(buf);
  1493. for (i = 0; i < num_regs; i++) {
  1494. int reg = regs[i].reg;
  1495. trace_regmap_hw_write_done(map, reg, 1);
  1496. }
  1497. return ret;
  1498. }
  1499. static unsigned int _regmap_register_page(struct regmap *map,
  1500. unsigned int reg,
  1501. struct regmap_range_node *range)
  1502. {
  1503. unsigned int win_page = (reg - range->range_min) / range->window_len;
  1504. return win_page;
  1505. }
  1506. static int _regmap_range_multi_paged_reg_write(struct regmap *map,
  1507. struct reg_default *regs,
  1508. size_t num_regs)
  1509. {
  1510. int ret;
  1511. int i, n;
  1512. struct reg_default *base;
  1513. unsigned int this_page = 0;
  1514. /*
  1515. * the set of registers are not neccessarily in order, but
  1516. * since the order of write must be preserved this algorithm
  1517. * chops the set each time the page changes
  1518. */
  1519. base = regs;
  1520. for (i = 0, n = 0; i < num_regs; i++, n++) {
  1521. unsigned int reg = regs[i].reg;
  1522. struct regmap_range_node *range;
  1523. range = _regmap_range_lookup(map, reg);
  1524. if (range) {
  1525. unsigned int win_page = _regmap_register_page(map, reg,
  1526. range);
  1527. if (i == 0)
  1528. this_page = win_page;
  1529. if (win_page != this_page) {
  1530. this_page = win_page;
  1531. ret = _regmap_raw_multi_reg_write(map, base, n);
  1532. if (ret != 0)
  1533. return ret;
  1534. base += n;
  1535. n = 0;
  1536. }
  1537. ret = _regmap_select_page(map, &base[n].reg, range, 1);
  1538. if (ret != 0)
  1539. return ret;
  1540. }
  1541. }
  1542. if (n > 0)
  1543. return _regmap_raw_multi_reg_write(map, base, n);
  1544. return 0;
  1545. }
  1546. static int _regmap_multi_reg_write(struct regmap *map,
  1547. const struct reg_default *regs,
  1548. size_t num_regs)
  1549. {
  1550. int i;
  1551. int ret;
  1552. if (!map->can_multi_write) {
  1553. for (i = 0; i < num_regs; i++) {
  1554. ret = _regmap_write(map, regs[i].reg, regs[i].def);
  1555. if (ret != 0)
  1556. return ret;
  1557. }
  1558. return 0;
  1559. }
  1560. if (!map->format.parse_inplace)
  1561. return -EINVAL;
  1562. if (map->writeable_reg)
  1563. for (i = 0; i < num_regs; i++) {
  1564. int reg = regs[i].reg;
  1565. if (!map->writeable_reg(map->dev, reg))
  1566. return -EINVAL;
  1567. if (reg % map->reg_stride)
  1568. return -EINVAL;
  1569. }
  1570. if (!map->cache_bypass) {
  1571. for (i = 0; i < num_regs; i++) {
  1572. unsigned int val = regs[i].def;
  1573. unsigned int reg = regs[i].reg;
  1574. ret = regcache_write(map, reg, val);
  1575. if (ret) {
  1576. dev_err(map->dev,
  1577. "Error in caching of register: %x ret: %d\n",
  1578. reg, ret);
  1579. return ret;
  1580. }
  1581. }
  1582. if (map->cache_only) {
  1583. map->cache_dirty = true;
  1584. return 0;
  1585. }
  1586. }
  1587. WARN_ON(!map->bus);
  1588. for (i = 0; i < num_regs; i++) {
  1589. unsigned int reg = regs[i].reg;
  1590. struct regmap_range_node *range;
  1591. range = _regmap_range_lookup(map, reg);
  1592. if (range) {
  1593. size_t len = sizeof(struct reg_default)*num_regs;
  1594. struct reg_default *base = kmemdup(regs, len,
  1595. GFP_KERNEL);
  1596. if (!base)
  1597. return -ENOMEM;
  1598. ret = _regmap_range_multi_paged_reg_write(map, base,
  1599. num_regs);
  1600. kfree(base);
  1601. return ret;
  1602. }
  1603. }
  1604. return _regmap_raw_multi_reg_write(map, regs, num_regs);
  1605. }
  1606. /*
  1607. * regmap_multi_reg_write(): Write multiple registers to the device
  1608. *
  1609. * where the set of register,value pairs are supplied in any order,
  1610. * possibly not all in a single range.
  1611. *
  1612. * @map: Register map to write to
  1613. * @regs: Array of structures containing register,value to be written
  1614. * @num_regs: Number of registers to write
  1615. *
  1616. * The 'normal' block write mode will send ultimately send data on the
  1617. * target bus as R,V1,V2,V3,..,Vn where successively higer registers are
  1618. * addressed. However, this alternative block multi write mode will send
  1619. * the data as R1,V1,R2,V2,..,Rn,Vn on the target bus. The target device
  1620. * must of course support the mode.
  1621. *
  1622. * A value of zero will be returned on success, a negative errno will be
  1623. * returned in error cases.
  1624. */
  1625. int regmap_multi_reg_write(struct regmap *map, const struct reg_default *regs,
  1626. int num_regs)
  1627. {
  1628. int ret;
  1629. map->lock(map->lock_arg);
  1630. ret = _regmap_multi_reg_write(map, regs, num_regs);
  1631. map->unlock(map->lock_arg);
  1632. return ret;
  1633. }
  1634. EXPORT_SYMBOL_GPL(regmap_multi_reg_write);
  1635. /*
  1636. * regmap_multi_reg_write_bypassed(): Write multiple registers to the
  1637. * device but not the cache
  1638. *
  1639. * where the set of register are supplied in any order
  1640. *
  1641. * @map: Register map to write to
  1642. * @regs: Array of structures containing register,value to be written
  1643. * @num_regs: Number of registers to write
  1644. *
  1645. * This function is intended to be used for writing a large block of data
  1646. * atomically to the device in single transfer for those I2C client devices
  1647. * that implement this alternative block write mode.
  1648. *
  1649. * A value of zero will be returned on success, a negative errno will
  1650. * be returned in error cases.
  1651. */
  1652. int regmap_multi_reg_write_bypassed(struct regmap *map,
  1653. const struct reg_default *regs,
  1654. int num_regs)
  1655. {
  1656. int ret;
  1657. bool bypass;
  1658. map->lock(map->lock_arg);
  1659. bypass = map->cache_bypass;
  1660. map->cache_bypass = true;
  1661. ret = _regmap_multi_reg_write(map, regs, num_regs);
  1662. map->cache_bypass = bypass;
  1663. map->unlock(map->lock_arg);
  1664. return ret;
  1665. }
  1666. EXPORT_SYMBOL_GPL(regmap_multi_reg_write_bypassed);
  1667. /**
  1668. * regmap_raw_write_async(): Write raw values to one or more registers
  1669. * asynchronously
  1670. *
  1671. * @map: Register map to write to
  1672. * @reg: Initial register to write to
  1673. * @val: Block of data to be written, laid out for direct transmission to the
  1674. * device. Must be valid until regmap_async_complete() is called.
  1675. * @val_len: Length of data pointed to by val.
  1676. *
  1677. * This function is intended to be used for things like firmware
  1678. * download where a large block of data needs to be transferred to the
  1679. * device. No formatting will be done on the data provided.
  1680. *
  1681. * If supported by the underlying bus the write will be scheduled
  1682. * asynchronously, helping maximise I/O speed on higher speed buses
  1683. * like SPI. regmap_async_complete() can be called to ensure that all
  1684. * asynchrnous writes have been completed.
  1685. *
  1686. * A value of zero will be returned on success, a negative errno will
  1687. * be returned in error cases.
  1688. */
  1689. int regmap_raw_write_async(struct regmap *map, unsigned int reg,
  1690. const void *val, size_t val_len)
  1691. {
  1692. int ret;
  1693. if (val_len % map->format.val_bytes)
  1694. return -EINVAL;
  1695. if (reg % map->reg_stride)
  1696. return -EINVAL;
  1697. map->lock(map->lock_arg);
  1698. map->async = true;
  1699. ret = _regmap_raw_write(map, reg, val, val_len);
  1700. map->async = false;
  1701. map->unlock(map->lock_arg);
  1702. return ret;
  1703. }
  1704. EXPORT_SYMBOL_GPL(regmap_raw_write_async);
  1705. static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
  1706. unsigned int val_len)
  1707. {
  1708. struct regmap_range_node *range;
  1709. u8 *u8 = map->work_buf;
  1710. int ret;
  1711. WARN_ON(!map->bus);
  1712. range = _regmap_range_lookup(map, reg);
  1713. if (range) {
  1714. ret = _regmap_select_page(map, &reg, range,
  1715. val_len / map->format.val_bytes);
  1716. if (ret != 0)
  1717. return ret;
  1718. }
  1719. map->format.format_reg(map->work_buf, reg, map->reg_shift);
  1720. /*
  1721. * Some buses or devices flag reads by setting the high bits in the
  1722. * register addresss; since it's always the high bits for all
  1723. * current formats we can do this here rather than in
  1724. * formatting. This may break if we get interesting formats.
  1725. */
  1726. u8[0] |= map->read_flag_mask;
  1727. trace_regmap_hw_read_start(map, reg, val_len / map->format.val_bytes);
  1728. ret = map->bus->read(map->bus_context, map->work_buf,
  1729. map->format.reg_bytes + map->format.pad_bytes,
  1730. val, val_len);
  1731. trace_regmap_hw_read_done(map, reg, val_len / map->format.val_bytes);
  1732. return ret;
  1733. }
  1734. static int _regmap_bus_reg_read(void *context, unsigned int reg,
  1735. unsigned int *val)
  1736. {
  1737. struct regmap *map = context;
  1738. return map->bus->reg_read(map->bus_context, reg, val);
  1739. }
  1740. static int _regmap_bus_read(void *context, unsigned int reg,
  1741. unsigned int *val)
  1742. {
  1743. int ret;
  1744. struct regmap *map = context;
  1745. if (!map->format.parse_val)
  1746. return -EINVAL;
  1747. ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
  1748. if (ret == 0)
  1749. *val = map->format.parse_val(map->work_buf);
  1750. return ret;
  1751. }
  1752. static int _regmap_read(struct regmap *map, unsigned int reg,
  1753. unsigned int *val)
  1754. {
  1755. int ret;
  1756. void *context = _regmap_map_get_context(map);
  1757. WARN_ON(!map->reg_read);
  1758. if (!map->cache_bypass) {
  1759. ret = regcache_read(map, reg, val);
  1760. if (ret == 0)
  1761. return 0;
  1762. }
  1763. if (map->cache_only)
  1764. return -EBUSY;
  1765. if (!regmap_readable(map, reg))
  1766. return -EIO;
  1767. ret = map->reg_read(context, reg, val);
  1768. if (ret == 0) {
  1769. #ifdef LOG_DEVICE
  1770. if (map->dev && strcmp(dev_name(map->dev), LOG_DEVICE) == 0)
  1771. dev_info(map->dev, "%x => %x\n", reg, *val);
  1772. #endif
  1773. trace_regmap_reg_read(map, reg, *val);
  1774. if (!map->cache_bypass)
  1775. regcache_write(map, reg, *val);
  1776. }
  1777. return ret;
  1778. }
  1779. /**
  1780. * regmap_read(): Read a value from a single register
  1781. *
  1782. * @map: Register map to read from
  1783. * @reg: Register to be read from
  1784. * @val: Pointer to store read value
  1785. *
  1786. * A value of zero will be returned on success, a negative errno will
  1787. * be returned in error cases.
  1788. */
  1789. int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
  1790. {
  1791. int ret;
  1792. if (reg % map->reg_stride)
  1793. return -EINVAL;
  1794. map->lock(map->lock_arg);
  1795. ret = _regmap_read(map, reg, val);
  1796. map->unlock(map->lock_arg);
  1797. return ret;
  1798. }
  1799. EXPORT_SYMBOL_GPL(regmap_read);
  1800. /**
  1801. * regmap_raw_read(): Read raw data from the device
  1802. *
  1803. * @map: Register map to read from
  1804. * @reg: First register to be read from
  1805. * @val: Pointer to store read value
  1806. * @val_len: Size of data to read
  1807. *
  1808. * A value of zero will be returned on success, a negative errno will
  1809. * be returned in error cases.
  1810. */
  1811. int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
  1812. size_t val_len)
  1813. {
  1814. size_t val_bytes = map->format.val_bytes;
  1815. size_t val_count = val_len / val_bytes;
  1816. unsigned int v;
  1817. int ret, i;
  1818. if (!map->bus)
  1819. return -EINVAL;
  1820. if (val_len % map->format.val_bytes)
  1821. return -EINVAL;
  1822. if (reg % map->reg_stride)
  1823. return -EINVAL;
  1824. map->lock(map->lock_arg);
  1825. if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
  1826. map->cache_type == REGCACHE_NONE) {
  1827. /* Physical block read if there's no cache involved */
  1828. ret = _regmap_raw_read(map, reg, val, val_len);
  1829. } else {
  1830. /* Otherwise go word by word for the cache; should be low
  1831. * cost as we expect to hit the cache.
  1832. */
  1833. for (i = 0; i < val_count; i++) {
  1834. ret = _regmap_read(map, reg + (i * map->reg_stride),
  1835. &v);
  1836. if (ret != 0)
  1837. goto out;
  1838. map->format.format_val(val + (i * val_bytes), v, 0);
  1839. }
  1840. }
  1841. out:
  1842. map->unlock(map->lock_arg);
  1843. return ret;
  1844. }
  1845. EXPORT_SYMBOL_GPL(regmap_raw_read);
  1846. /**
  1847. * regmap_field_read(): Read a value to a single register field
  1848. *
  1849. * @field: Register field to read from
  1850. * @val: Pointer to store read value
  1851. *
  1852. * A value of zero will be returned on success, a negative errno will
  1853. * be returned in error cases.
  1854. */
  1855. int regmap_field_read(struct regmap_field *field, unsigned int *val)
  1856. {
  1857. int ret;
  1858. unsigned int reg_val;
  1859. ret = regmap_read(field->regmap, field->reg, &reg_val);
  1860. if (ret != 0)
  1861. return ret;
  1862. reg_val &= field->mask;
  1863. reg_val >>= field->shift;
  1864. *val = reg_val;
  1865. return ret;
  1866. }
  1867. EXPORT_SYMBOL_GPL(regmap_field_read);
  1868. /**
  1869. * regmap_fields_read(): Read a value to a single register field with port ID
  1870. *
  1871. * @field: Register field to read from
  1872. * @id: port ID
  1873. * @val: Pointer to store read value
  1874. *
  1875. * A value of zero will be returned on success, a negative errno will
  1876. * be returned in error cases.
  1877. */
  1878. int regmap_fields_read(struct regmap_field *field, unsigned int id,
  1879. unsigned int *val)
  1880. {
  1881. int ret;
  1882. unsigned int reg_val;
  1883. if (id >= field->id_size)
  1884. return -EINVAL;
  1885. ret = regmap_read(field->regmap,
  1886. field->reg + (field->id_offset * id),
  1887. &reg_val);
  1888. if (ret != 0)
  1889. return ret;
  1890. reg_val &= field->mask;
  1891. reg_val >>= field->shift;
  1892. *val = reg_val;
  1893. return ret;
  1894. }
  1895. EXPORT_SYMBOL_GPL(regmap_fields_read);
  1896. /**
  1897. * regmap_bulk_read(): Read multiple registers from the device
  1898. *
  1899. * @map: Register map to read from
  1900. * @reg: First register to be read from
  1901. * @val: Pointer to store read value, in native register size for device
  1902. * @val_count: Number of registers to read
  1903. *
  1904. * A value of zero will be returned on success, a negative errno will
  1905. * be returned in error cases.
  1906. */
  1907. int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
  1908. size_t val_count)
  1909. {
  1910. int ret, i;
  1911. size_t val_bytes = map->format.val_bytes;
  1912. bool vol = regmap_volatile_range(map, reg, val_count);
  1913. if (reg % map->reg_stride)
  1914. return -EINVAL;
  1915. if (map->bus && map->format.parse_inplace && (vol || map->cache_type == REGCACHE_NONE)) {
  1916. /*
  1917. * Some devices does not support bulk read, for
  1918. * them we have a series of single read operations.
  1919. */
  1920. if (map->use_single_rw) {
  1921. for (i = 0; i < val_count; i++) {
  1922. ret = regmap_raw_read(map,
  1923. reg + (i * map->reg_stride),
  1924. val + (i * val_bytes),
  1925. val_bytes);
  1926. if (ret != 0)
  1927. return ret;
  1928. }
  1929. } else {
  1930. ret = regmap_raw_read(map, reg, val,
  1931. val_bytes * val_count);
  1932. if (ret != 0)
  1933. return ret;
  1934. }
  1935. for (i = 0; i < val_count * val_bytes; i += val_bytes)
  1936. map->format.parse_inplace(val + i);
  1937. } else {
  1938. for (i = 0; i < val_count; i++) {
  1939. unsigned int ival;
  1940. ret = regmap_read(map, reg + (i * map->reg_stride),
  1941. &ival);
  1942. if (ret != 0)
  1943. return ret;
  1944. map->format.format_val(val + (i * val_bytes), ival, 0);
  1945. }
  1946. }
  1947. return 0;
  1948. }
  1949. EXPORT_SYMBOL_GPL(regmap_bulk_read);
  1950. static int _regmap_update_bits(struct regmap *map, unsigned int reg,
  1951. unsigned int mask, unsigned int val,
  1952. bool *change)
  1953. {
  1954. int ret;
  1955. unsigned int tmp, orig;
  1956. ret = _regmap_read(map, reg, &orig);
  1957. if (ret != 0)
  1958. return ret;
  1959. tmp = orig & ~mask;
  1960. tmp |= val & mask;
  1961. if (tmp != orig) {
  1962. ret = _regmap_write(map, reg, tmp);
  1963. if (change)
  1964. *change = true;
  1965. } else {
  1966. if (change)
  1967. *change = false;
  1968. }
  1969. return ret;
  1970. }
  1971. /**
  1972. * regmap_update_bits: Perform a read/modify/write cycle on the register map
  1973. *
  1974. * @map: Register map to update
  1975. * @reg: Register to update
  1976. * @mask: Bitmask to change
  1977. * @val: New value for bitmask
  1978. *
  1979. * Returns zero for success, a negative number on error.
  1980. */
  1981. int regmap_update_bits(struct regmap *map, unsigned int reg,
  1982. unsigned int mask, unsigned int val)
  1983. {
  1984. int ret;
  1985. map->lock(map->lock_arg);
  1986. ret = _regmap_update_bits(map, reg, mask, val, NULL);
  1987. map->unlock(map->lock_arg);
  1988. return ret;
  1989. }
  1990. EXPORT_SYMBOL_GPL(regmap_update_bits);
  1991. /**
  1992. * regmap_update_bits_async: Perform a read/modify/write cycle on the register
  1993. * map asynchronously
  1994. *
  1995. * @map: Register map to update
  1996. * @reg: Register to update
  1997. * @mask: Bitmask to change
  1998. * @val: New value for bitmask
  1999. *
  2000. * With most buses the read must be done synchronously so this is most
  2001. * useful for devices with a cache which do not need to interact with
  2002. * the hardware to determine the current register value.
  2003. *
  2004. * Returns zero for success, a negative number on error.
  2005. */
  2006. int regmap_update_bits_async(struct regmap *map, unsigned int reg,
  2007. unsigned int mask, unsigned int val)
  2008. {
  2009. int ret;
  2010. map->lock(map->lock_arg);
  2011. map->async = true;
  2012. ret = _regmap_update_bits(map, reg, mask, val, NULL);
  2013. map->async = false;
  2014. map->unlock(map->lock_arg);
  2015. return ret;
  2016. }
  2017. EXPORT_SYMBOL_GPL(regmap_update_bits_async);
  2018. /**
  2019. * regmap_update_bits_check: Perform a read/modify/write cycle on the
  2020. * register map and report if updated
  2021. *
  2022. * @map: Register map to update
  2023. * @reg: Register to update
  2024. * @mask: Bitmask to change
  2025. * @val: New value for bitmask
  2026. * @change: Boolean indicating if a write was done
  2027. *
  2028. * Returns zero for success, a negative number on error.
  2029. */
  2030. int regmap_update_bits_check(struct regmap *map, unsigned int reg,
  2031. unsigned int mask, unsigned int val,
  2032. bool *change)
  2033. {
  2034. int ret;
  2035. map->lock(map->lock_arg);
  2036. ret = _regmap_update_bits(map, reg, mask, val, change);
  2037. map->unlock(map->lock_arg);
  2038. return ret;
  2039. }
  2040. EXPORT_SYMBOL_GPL(regmap_update_bits_check);
  2041. /**
  2042. * regmap_update_bits_check_async: Perform a read/modify/write cycle on the
  2043. * register map asynchronously and report if
  2044. * updated
  2045. *
  2046. * @map: Register map to update
  2047. * @reg: Register to update
  2048. * @mask: Bitmask to change
  2049. * @val: New value for bitmask
  2050. * @change: Boolean indicating if a write was done
  2051. *
  2052. * With most buses the read must be done synchronously so this is most
  2053. * useful for devices with a cache which do not need to interact with
  2054. * the hardware to determine the current register value.
  2055. *
  2056. * Returns zero for success, a negative number on error.
  2057. */
  2058. int regmap_update_bits_check_async(struct regmap *map, unsigned int reg,
  2059. unsigned int mask, unsigned int val,
  2060. bool *change)
  2061. {
  2062. int ret;
  2063. map->lock(map->lock_arg);
  2064. map->async = true;
  2065. ret = _regmap_update_bits(map, reg, mask, val, change);
  2066. map->async = false;
  2067. map->unlock(map->lock_arg);
  2068. return ret;
  2069. }
  2070. EXPORT_SYMBOL_GPL(regmap_update_bits_check_async);
  2071. void regmap_async_complete_cb(struct regmap_async *async, int ret)
  2072. {
  2073. struct regmap *map = async->map;
  2074. bool wake;
  2075. trace_regmap_async_io_complete(map);
  2076. spin_lock(&map->async_lock);
  2077. list_move(&async->list, &map->async_free);
  2078. wake = list_empty(&map->async_list);
  2079. if (ret != 0)
  2080. map->async_ret = ret;
  2081. spin_unlock(&map->async_lock);
  2082. if (wake)
  2083. wake_up(&map->async_waitq);
  2084. }
  2085. EXPORT_SYMBOL_GPL(regmap_async_complete_cb);
  2086. static int regmap_async_is_done(struct regmap *map)
  2087. {
  2088. unsigned long flags;
  2089. int ret;
  2090. spin_lock_irqsave(&map->async_lock, flags);
  2091. ret = list_empty(&map->async_list);
  2092. spin_unlock_irqrestore(&map->async_lock, flags);
  2093. return ret;
  2094. }
  2095. /**
  2096. * regmap_async_complete: Ensure all asynchronous I/O has completed.
  2097. *
  2098. * @map: Map to operate on.
  2099. *
  2100. * Blocks until any pending asynchronous I/O has completed. Returns
  2101. * an error code for any failed I/O operations.
  2102. */
  2103. int regmap_async_complete(struct regmap *map)
  2104. {
  2105. unsigned long flags;
  2106. int ret;
  2107. /* Nothing to do with no async support */
  2108. if (!map->bus || !map->bus->async_write)
  2109. return 0;
  2110. trace_regmap_async_complete_start(map);
  2111. wait_event(map->async_waitq, regmap_async_is_done(map));
  2112. spin_lock_irqsave(&map->async_lock, flags);
  2113. ret = map->async_ret;
  2114. map->async_ret = 0;
  2115. spin_unlock_irqrestore(&map->async_lock, flags);
  2116. trace_regmap_async_complete_done(map);
  2117. return ret;
  2118. }
  2119. EXPORT_SYMBOL_GPL(regmap_async_complete);
  2120. /**
  2121. * regmap_register_patch: Register and apply register updates to be applied
  2122. * on device initialistion
  2123. *
  2124. * @map: Register map to apply updates to.
  2125. * @regs: Values to update.
  2126. * @num_regs: Number of entries in regs.
  2127. *
  2128. * Register a set of register updates to be applied to the device
  2129. * whenever the device registers are synchronised with the cache and
  2130. * apply them immediately. Typically this is used to apply
  2131. * corrections to be applied to the device defaults on startup, such
  2132. * as the updates some vendors provide to undocumented registers.
  2133. *
  2134. * The caller must ensure that this function cannot be called
  2135. * concurrently with either itself or regcache_sync().
  2136. */
  2137. int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
  2138. int num_regs)
  2139. {
  2140. struct reg_default *p;
  2141. int ret;
  2142. bool bypass;
  2143. if (WARN_ONCE(num_regs <= 0, "invalid registers number (%d)\n",
  2144. num_regs))
  2145. return 0;
  2146. p = krealloc(map->patch,
  2147. sizeof(struct reg_default) * (map->patch_regs + num_regs),
  2148. GFP_KERNEL);
  2149. if (p) {
  2150. memcpy(p + map->patch_regs, regs, num_regs * sizeof(*regs));
  2151. map->patch = p;
  2152. map->patch_regs += num_regs;
  2153. } else {
  2154. return -ENOMEM;
  2155. }
  2156. map->lock(map->lock_arg);
  2157. bypass = map->cache_bypass;
  2158. map->cache_bypass = true;
  2159. map->async = true;
  2160. ret = _regmap_multi_reg_write(map, regs, num_regs);
  2161. if (ret != 0)
  2162. goto out;
  2163. out:
  2164. map->async = false;
  2165. map->cache_bypass = bypass;
  2166. map->unlock(map->lock_arg);
  2167. regmap_async_complete(map);
  2168. return ret;
  2169. }
  2170. EXPORT_SYMBOL_GPL(regmap_register_patch);
  2171. /*
  2172. * regmap_get_val_bytes(): Report the size of a register value
  2173. *
  2174. * Report the size of a register value, mainly intended to for use by
  2175. * generic infrastructure built on top of regmap.
  2176. */
  2177. int regmap_get_val_bytes(struct regmap *map)
  2178. {
  2179. if (map->format.format_write)
  2180. return -EINVAL;
  2181. return map->format.val_bytes;
  2182. }
  2183. EXPORT_SYMBOL_GPL(regmap_get_val_bytes);
  2184. int regmap_parse_val(struct regmap *map, const void *buf,
  2185. unsigned int *val)
  2186. {
  2187. if (!map->format.parse_val)
  2188. return -EINVAL;
  2189. *val = map->format.parse_val(buf);
  2190. return 0;
  2191. }
  2192. EXPORT_SYMBOL_GPL(regmap_parse_val);
  2193. static int __init regmap_initcall(void)
  2194. {
  2195. regmap_debugfs_initcall();
  2196. return 0;
  2197. }
  2198. postcore_initcall(regmap_initcall);