regmap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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/slab.h>
  13. #include <linux/module.h>
  14. #include <linux/mutex.h>
  15. #include <linux/err.h>
  16. #include <linux/regmap.h>
  17. struct regmap;
  18. struct regmap_format {
  19. size_t buf_size;
  20. size_t reg_bytes;
  21. size_t val_bytes;
  22. void (*format_write)(struct regmap *map,
  23. unsigned int reg, unsigned int val);
  24. void (*format_reg)(void *buf, unsigned int reg);
  25. void (*format_val)(void *buf, unsigned int val);
  26. unsigned int (*parse_val)(void *buf);
  27. };
  28. struct regmap {
  29. struct mutex lock;
  30. struct device *dev; /* Device we do I/O on */
  31. void *work_buf; /* Scratch buffer used to format I/O */
  32. struct regmap_format format; /* Buffer format */
  33. const struct regmap_bus *bus;
  34. unsigned int max_register;
  35. bool (*writeable_reg)(struct device *dev, unsigned int reg);
  36. bool (*readable_reg)(struct device *dev, unsigned int reg);
  37. bool (*volatile_reg)(struct device *dev, unsigned int reg);
  38. };
  39. static void regmap_format_4_12_write(struct regmap *map,
  40. unsigned int reg, unsigned int val)
  41. {
  42. __be16 *out = map->work_buf;
  43. *out = cpu_to_be16((reg << 12) | val);
  44. }
  45. static void regmap_format_7_9_write(struct regmap *map,
  46. unsigned int reg, unsigned int val)
  47. {
  48. __be16 *out = map->work_buf;
  49. *out = cpu_to_be16((reg << 9) | val);
  50. }
  51. static void regmap_format_8(void *buf, unsigned int val)
  52. {
  53. u8 *b = buf;
  54. b[0] = val;
  55. }
  56. static void regmap_format_16(void *buf, unsigned int val)
  57. {
  58. __be16 *b = buf;
  59. b[0] = cpu_to_be16(val);
  60. }
  61. static unsigned int regmap_parse_8(void *buf)
  62. {
  63. u8 *b = buf;
  64. return b[0];
  65. }
  66. static unsigned int regmap_parse_16(void *buf)
  67. {
  68. __be16 *b = buf;
  69. b[0] = be16_to_cpu(b[0]);
  70. return b[0];
  71. }
  72. /**
  73. * regmap_init(): Initialise register map
  74. *
  75. * @dev: Device that will be interacted with
  76. * @bus: Bus-specific callbacks to use with device
  77. * @config: Configuration for register map
  78. *
  79. * The return value will be an ERR_PTR() on error or a valid pointer to
  80. * a struct regmap. This function should generally not be called
  81. * directly, it should be called by bus-specific init functions.
  82. */
  83. struct regmap *regmap_init(struct device *dev,
  84. const struct regmap_bus *bus,
  85. const struct regmap_config *config)
  86. {
  87. struct regmap *map;
  88. int ret = -EINVAL;
  89. if (!bus || !config)
  90. return NULL;
  91. map = kzalloc(sizeof(*map), GFP_KERNEL);
  92. if (map == NULL) {
  93. ret = -ENOMEM;
  94. goto err;
  95. }
  96. mutex_init(&map->lock);
  97. map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
  98. map->format.reg_bytes = config->reg_bits / 8;
  99. map->format.val_bytes = config->val_bits / 8;
  100. map->dev = dev;
  101. map->bus = bus;
  102. map->max_register = config->max_register;
  103. map->writeable_reg = config->writeable_reg;
  104. map->readable_reg = config->readable_reg;
  105. map->volatile_reg = config->volatile_reg;
  106. switch (config->reg_bits) {
  107. case 4:
  108. switch (config->val_bits) {
  109. case 12:
  110. map->format.format_write = regmap_format_4_12_write;
  111. break;
  112. default:
  113. goto err_map;
  114. }
  115. break;
  116. case 7:
  117. switch (config->val_bits) {
  118. case 9:
  119. map->format.format_write = regmap_format_7_9_write;
  120. break;
  121. default:
  122. goto err_map;
  123. }
  124. break;
  125. case 8:
  126. map->format.format_reg = regmap_format_8;
  127. break;
  128. case 16:
  129. map->format.format_reg = regmap_format_16;
  130. break;
  131. default:
  132. goto err_map;
  133. }
  134. switch (config->val_bits) {
  135. case 8:
  136. map->format.format_val = regmap_format_8;
  137. map->format.parse_val = regmap_parse_8;
  138. break;
  139. case 16:
  140. map->format.format_val = regmap_format_16;
  141. map->format.parse_val = regmap_parse_16;
  142. break;
  143. }
  144. if (!map->format.format_write &&
  145. !(map->format.format_reg && map->format.format_val))
  146. goto err_map;
  147. map->work_buf = kmalloc(map->format.buf_size, GFP_KERNEL);
  148. if (map->work_buf == NULL) {
  149. ret = -ENOMEM;
  150. goto err_bus;
  151. }
  152. return map;
  153. err_bus:
  154. module_put(map->bus->owner);
  155. err_map:
  156. kfree(map);
  157. err:
  158. return ERR_PTR(ret);
  159. }
  160. EXPORT_SYMBOL_GPL(regmap_init);
  161. /**
  162. * regmap_exit(): Free a previously allocated register map
  163. */
  164. void regmap_exit(struct regmap *map)
  165. {
  166. kfree(map->work_buf);
  167. module_put(map->bus->owner);
  168. kfree(map);
  169. }
  170. EXPORT_SYMBOL_GPL(regmap_exit);
  171. static int _regmap_raw_write(struct regmap *map, unsigned int reg,
  172. const void *val, size_t val_len)
  173. {
  174. void *buf;
  175. int ret = -ENOTSUPP;
  176. size_t len;
  177. map->format.format_reg(map->work_buf, reg);
  178. /* If we're doing a single register write we can probably just
  179. * send the work_buf directly, otherwise try to do a gather
  180. * write.
  181. */
  182. if (val == map->work_buf + map->format.reg_bytes)
  183. ret = map->bus->write(map->dev, map->work_buf,
  184. map->format.reg_bytes + val_len);
  185. else if (map->bus->gather_write)
  186. ret = map->bus->gather_write(map->dev, map->work_buf,
  187. map->format.reg_bytes,
  188. val, val_len);
  189. /* If that didn't work fall back on linearising by hand. */
  190. if (ret == -ENOTSUPP) {
  191. len = map->format.reg_bytes + val_len;
  192. buf = kmalloc(len, GFP_KERNEL);
  193. if (!buf)
  194. return -ENOMEM;
  195. memcpy(buf, map->work_buf, map->format.reg_bytes);
  196. memcpy(buf + map->format.reg_bytes, val, val_len);
  197. ret = map->bus->write(map->dev, buf, len);
  198. kfree(buf);
  199. }
  200. return ret;
  201. }
  202. static int _regmap_write(struct regmap *map, unsigned int reg,
  203. unsigned int val)
  204. {
  205. BUG_ON(!map->format.format_write && !map->format.format_val);
  206. if (map->format.format_write) {
  207. map->format.format_write(map, reg, val);
  208. return map->bus->write(map->dev, map->work_buf,
  209. map->format.buf_size);
  210. } else {
  211. map->format.format_val(map->work_buf + map->format.reg_bytes,
  212. val);
  213. return _regmap_raw_write(map, reg,
  214. map->work_buf + map->format.reg_bytes,
  215. map->format.val_bytes);
  216. }
  217. }
  218. /**
  219. * regmap_write(): Write a value to a single register
  220. *
  221. * @map: Register map to write to
  222. * @reg: Register to write to
  223. * @val: Value to be written
  224. *
  225. * A value of zero will be returned on success, a negative errno will
  226. * be returned in error cases.
  227. */
  228. int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
  229. {
  230. int ret;
  231. mutex_lock(&map->lock);
  232. ret = _regmap_write(map, reg, val);
  233. mutex_unlock(&map->lock);
  234. return ret;
  235. }
  236. EXPORT_SYMBOL_GPL(regmap_write);
  237. /**
  238. * regmap_raw_write(): Write raw values to one or more registers
  239. *
  240. * @map: Register map to write to
  241. * @reg: Initial register to write to
  242. * @val: Block of data to be written, laid out for direct transmission to the
  243. * device
  244. * @val_len: Length of data pointed to by val.
  245. *
  246. * This function is intended to be used for things like firmware
  247. * download where a large block of data needs to be transferred to the
  248. * device. No formatting will be done on the data provided.
  249. *
  250. * A value of zero will be returned on success, a negative errno will
  251. * be returned in error cases.
  252. */
  253. int regmap_raw_write(struct regmap *map, unsigned int reg,
  254. const void *val, size_t val_len)
  255. {
  256. int ret;
  257. mutex_lock(&map->lock);
  258. ret = _regmap_raw_write(map, reg, val, val_len);
  259. mutex_unlock(&map->lock);
  260. return ret;
  261. }
  262. EXPORT_SYMBOL_GPL(regmap_raw_write);
  263. static int _regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
  264. unsigned int val_len)
  265. {
  266. u8 *u8 = map->work_buf;
  267. int ret;
  268. map->format.format_reg(map->work_buf, reg);
  269. /*
  270. * Some buses flag reads by setting the high bits in the
  271. * register addresss; since it's always the high bits for all
  272. * current formats we can do this here rather than in
  273. * formatting. This may break if we get interesting formats.
  274. */
  275. if (map->bus->read_flag_mask)
  276. u8[0] |= map->bus->read_flag_mask;
  277. ret = map->bus->read(map->dev, map->work_buf, map->format.reg_bytes,
  278. val, val_len);
  279. if (ret != 0)
  280. return ret;
  281. return 0;
  282. }
  283. static int _regmap_read(struct regmap *map, unsigned int reg,
  284. unsigned int *val)
  285. {
  286. int ret;
  287. if (!map->format.parse_val)
  288. return -EINVAL;
  289. ret = _regmap_raw_read(map, reg, map->work_buf, map->format.val_bytes);
  290. if (ret == 0)
  291. *val = map->format.parse_val(map->work_buf);
  292. return ret;
  293. }
  294. /**
  295. * regmap_read(): Read a value from a single register
  296. *
  297. * @map: Register map to write to
  298. * @reg: Register to be read from
  299. * @val: Pointer to store read value
  300. *
  301. * A value of zero will be returned on success, a negative errno will
  302. * be returned in error cases.
  303. */
  304. int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
  305. {
  306. int ret;
  307. mutex_lock(&map->lock);
  308. ret = _regmap_read(map, reg, val);
  309. mutex_unlock(&map->lock);
  310. return ret;
  311. }
  312. EXPORT_SYMBOL_GPL(regmap_read);
  313. /**
  314. * regmap_raw_read(): Read raw data from the device
  315. *
  316. * @map: Register map to write to
  317. * @reg: First register to be read from
  318. * @val: Pointer to store read value
  319. * @val_len: Size of data to read
  320. *
  321. * A value of zero will be returned on success, a negative errno will
  322. * be returned in error cases.
  323. */
  324. int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
  325. size_t val_len)
  326. {
  327. int ret;
  328. mutex_lock(&map->lock);
  329. ret = _regmap_raw_read(map, reg, val, val_len);
  330. mutex_unlock(&map->lock);
  331. return ret;
  332. }
  333. EXPORT_SYMBOL_GPL(regmap_raw_read);
  334. /**
  335. * regmap_bulk_read(): Read multiple registers from the device
  336. *
  337. * @map: Register map to write to
  338. * @reg: First register to be read from
  339. * @val: Pointer to store read value, in native register size for device
  340. * @val_count: Number of registers to read
  341. *
  342. * A value of zero will be returned on success, a negative errno will
  343. * be returned in error cases.
  344. */
  345. int regmap_bulk_read(struct regmap *map, unsigned int reg, void *val,
  346. size_t val_count)
  347. {
  348. int ret, i;
  349. size_t val_bytes = map->format.val_bytes;
  350. if (!map->format.parse_val)
  351. return -EINVAL;
  352. ret = regmap_raw_read(map, reg, val, val_bytes * val_count);
  353. if (ret != 0)
  354. return ret;
  355. for (i = 0; i < val_count * val_bytes; i += val_bytes)
  356. map->format.parse_val(val + i);
  357. return 0;
  358. }
  359. EXPORT_SYMBOL_GPL(regmap_bulk_read);
  360. /**
  361. * remap_update_bits: Perform a read/modify/write cycle on the register map
  362. *
  363. * @map: Register map to update
  364. * @reg: Register to update
  365. * @mask: Bitmask to change
  366. * @val: New value for bitmask
  367. *
  368. * Returns zero for success, a negative number on error.
  369. */
  370. int regmap_update_bits(struct regmap *map, unsigned int reg,
  371. unsigned int mask, unsigned int val)
  372. {
  373. int ret;
  374. unsigned int tmp;
  375. mutex_lock(&map->lock);
  376. ret = _regmap_read(map, reg, &tmp);
  377. if (ret != 0)
  378. goto out;
  379. tmp &= ~mask;
  380. tmp |= val & mask;
  381. ret = _regmap_write(map, reg, tmp);
  382. out:
  383. mutex_unlock(&map->lock);
  384. return ret;
  385. }
  386. EXPORT_SYMBOL_GPL(regmap_update_bits);