regmap.c 50 KB

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