regmap.c 72 KB

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