regmap.c 47 KB

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