core.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * nvmem framework core.
  4. *
  5. * Copyright (C) 2015 Srinivas Kandagatla <[email protected]>
  6. * Copyright (C) 2013 Maxime Ripard <[email protected]>
  7. */
  8. #include <linux/device.h>
  9. #include <linux/export.h>
  10. #include <linux/fs.h>
  11. #include <linux/idr.h>
  12. #include <linux/init.h>
  13. #include <linux/kref.h>
  14. #include <linux/module.h>
  15. #include <linux/nvmem-consumer.h>
  16. #include <linux/nvmem-provider.h>
  17. #include <linux/gpio/consumer.h>
  18. #include <linux/of.h>
  19. #include <linux/slab.h>
  20. struct nvmem_device {
  21. struct module *owner;
  22. struct device dev;
  23. int stride;
  24. int word_size;
  25. int id;
  26. struct kref refcnt;
  27. size_t size;
  28. bool read_only;
  29. bool root_only;
  30. int flags;
  31. enum nvmem_type type;
  32. struct bin_attribute eeprom;
  33. struct device *base_dev;
  34. struct list_head cells;
  35. const struct nvmem_keepout *keepout;
  36. unsigned int nkeepout;
  37. nvmem_reg_read_t reg_read;
  38. nvmem_reg_write_t reg_write;
  39. nvmem_cell_post_process_t cell_post_process;
  40. struct gpio_desc *wp_gpio;
  41. void *priv;
  42. };
  43. #define to_nvmem_device(d) container_of(d, struct nvmem_device, dev)
  44. #define FLAG_COMPAT BIT(0)
  45. struct nvmem_cell_entry {
  46. const char *name;
  47. int offset;
  48. int bytes;
  49. int bit_offset;
  50. int nbits;
  51. struct device_node *np;
  52. struct nvmem_device *nvmem;
  53. struct list_head node;
  54. };
  55. struct nvmem_cell {
  56. struct nvmem_cell_entry *entry;
  57. const char *id;
  58. };
  59. static DEFINE_MUTEX(nvmem_mutex);
  60. static DEFINE_IDA(nvmem_ida);
  61. static DEFINE_MUTEX(nvmem_cell_mutex);
  62. static LIST_HEAD(nvmem_cell_tables);
  63. static DEFINE_MUTEX(nvmem_lookup_mutex);
  64. static LIST_HEAD(nvmem_lookup_list);
  65. static BLOCKING_NOTIFIER_HEAD(nvmem_notifier);
  66. static int __nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
  67. void *val, size_t bytes)
  68. {
  69. if (nvmem->reg_read)
  70. return nvmem->reg_read(nvmem->priv, offset, val, bytes);
  71. return -EINVAL;
  72. }
  73. static int __nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
  74. void *val, size_t bytes)
  75. {
  76. int ret;
  77. if (nvmem->reg_write) {
  78. gpiod_set_value_cansleep(nvmem->wp_gpio, 0);
  79. ret = nvmem->reg_write(nvmem->priv, offset, val, bytes);
  80. gpiod_set_value_cansleep(nvmem->wp_gpio, 1);
  81. return ret;
  82. }
  83. return -EINVAL;
  84. }
  85. static int nvmem_access_with_keepouts(struct nvmem_device *nvmem,
  86. unsigned int offset, void *val,
  87. size_t bytes, int write)
  88. {
  89. unsigned int end = offset + bytes;
  90. unsigned int kend, ksize;
  91. const struct nvmem_keepout *keepout = nvmem->keepout;
  92. const struct nvmem_keepout *keepoutend = keepout + nvmem->nkeepout;
  93. int rc;
  94. /*
  95. * Skip all keepouts before the range being accessed.
  96. * Keepouts are sorted.
  97. */
  98. while ((keepout < keepoutend) && (keepout->end <= offset))
  99. keepout++;
  100. while ((offset < end) && (keepout < keepoutend)) {
  101. /* Access the valid portion before the keepout. */
  102. if (offset < keepout->start) {
  103. kend = min(end, keepout->start);
  104. ksize = kend - offset;
  105. if (write)
  106. rc = __nvmem_reg_write(nvmem, offset, val, ksize);
  107. else
  108. rc = __nvmem_reg_read(nvmem, offset, val, ksize);
  109. if (rc)
  110. return rc;
  111. offset += ksize;
  112. val += ksize;
  113. }
  114. /*
  115. * Now we're aligned to the start of this keepout zone. Go
  116. * through it.
  117. */
  118. kend = min(end, keepout->end);
  119. ksize = kend - offset;
  120. if (!write)
  121. memset(val, keepout->value, ksize);
  122. val += ksize;
  123. offset += ksize;
  124. keepout++;
  125. }
  126. /*
  127. * If we ran out of keepouts but there's still stuff to do, send it
  128. * down directly
  129. */
  130. if (offset < end) {
  131. ksize = end - offset;
  132. if (write)
  133. return __nvmem_reg_write(nvmem, offset, val, ksize);
  134. else
  135. return __nvmem_reg_read(nvmem, offset, val, ksize);
  136. }
  137. return 0;
  138. }
  139. static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
  140. void *val, size_t bytes)
  141. {
  142. if (!nvmem->nkeepout)
  143. return __nvmem_reg_read(nvmem, offset, val, bytes);
  144. return nvmem_access_with_keepouts(nvmem, offset, val, bytes, false);
  145. }
  146. static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
  147. void *val, size_t bytes)
  148. {
  149. if (!nvmem->nkeepout)
  150. return __nvmem_reg_write(nvmem, offset, val, bytes);
  151. return nvmem_access_with_keepouts(nvmem, offset, val, bytes, true);
  152. }
  153. #ifdef CONFIG_NVMEM_SYSFS
  154. static const char * const nvmem_type_str[] = {
  155. [NVMEM_TYPE_UNKNOWN] = "Unknown",
  156. [NVMEM_TYPE_EEPROM] = "EEPROM",
  157. [NVMEM_TYPE_OTP] = "OTP",
  158. [NVMEM_TYPE_BATTERY_BACKED] = "Battery backed",
  159. [NVMEM_TYPE_FRAM] = "FRAM",
  160. };
  161. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  162. static struct lock_class_key eeprom_lock_key;
  163. #endif
  164. static ssize_t type_show(struct device *dev,
  165. struct device_attribute *attr, char *buf)
  166. {
  167. struct nvmem_device *nvmem = to_nvmem_device(dev);
  168. return sprintf(buf, "%s\n", nvmem_type_str[nvmem->type]);
  169. }
  170. static DEVICE_ATTR_RO(type);
  171. static struct attribute *nvmem_attrs[] = {
  172. &dev_attr_type.attr,
  173. NULL,
  174. };
  175. static ssize_t bin_attr_nvmem_read(struct file *filp, struct kobject *kobj,
  176. struct bin_attribute *attr, char *buf,
  177. loff_t pos, size_t count)
  178. {
  179. struct device *dev;
  180. struct nvmem_device *nvmem;
  181. int rc;
  182. if (attr->private)
  183. dev = attr->private;
  184. else
  185. dev = kobj_to_dev(kobj);
  186. nvmem = to_nvmem_device(dev);
  187. /* Stop the user from reading */
  188. if (pos >= nvmem->size)
  189. return 0;
  190. if (!IS_ALIGNED(pos, nvmem->stride))
  191. return -EINVAL;
  192. if (count < nvmem->word_size)
  193. return -EINVAL;
  194. if (pos + count > nvmem->size)
  195. count = nvmem->size - pos;
  196. count = round_down(count, nvmem->word_size);
  197. if (!nvmem->reg_read)
  198. return -EPERM;
  199. rc = nvmem_reg_read(nvmem, pos, buf, count);
  200. if (rc)
  201. return rc;
  202. return count;
  203. }
  204. static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj,
  205. struct bin_attribute *attr, char *buf,
  206. loff_t pos, size_t count)
  207. {
  208. struct device *dev;
  209. struct nvmem_device *nvmem;
  210. int rc;
  211. if (attr->private)
  212. dev = attr->private;
  213. else
  214. dev = kobj_to_dev(kobj);
  215. nvmem = to_nvmem_device(dev);
  216. /* Stop the user from writing */
  217. if (pos >= nvmem->size)
  218. return -EFBIG;
  219. if (!IS_ALIGNED(pos, nvmem->stride))
  220. return -EINVAL;
  221. if (count < nvmem->word_size)
  222. return -EINVAL;
  223. if (pos + count > nvmem->size)
  224. count = nvmem->size - pos;
  225. count = round_down(count, nvmem->word_size);
  226. if (!nvmem->reg_write)
  227. return -EPERM;
  228. rc = nvmem_reg_write(nvmem, pos, buf, count);
  229. if (rc)
  230. return rc;
  231. return count;
  232. }
  233. static umode_t nvmem_bin_attr_get_umode(struct nvmem_device *nvmem)
  234. {
  235. umode_t mode = 0400;
  236. if (!nvmem->root_only)
  237. mode |= 0044;
  238. if (!nvmem->read_only)
  239. mode |= 0200;
  240. if (!nvmem->reg_write)
  241. mode &= ~0200;
  242. if (!nvmem->reg_read)
  243. mode &= ~0444;
  244. return mode;
  245. }
  246. static umode_t nvmem_bin_attr_is_visible(struct kobject *kobj,
  247. struct bin_attribute *attr, int i)
  248. {
  249. struct device *dev = kobj_to_dev(kobj);
  250. struct nvmem_device *nvmem = to_nvmem_device(dev);
  251. attr->size = nvmem->size;
  252. return nvmem_bin_attr_get_umode(nvmem);
  253. }
  254. /* default read/write permissions */
  255. static struct bin_attribute bin_attr_rw_nvmem = {
  256. .attr = {
  257. .name = "nvmem",
  258. .mode = 0644,
  259. },
  260. .read = bin_attr_nvmem_read,
  261. .write = bin_attr_nvmem_write,
  262. };
  263. static struct bin_attribute *nvmem_bin_attributes[] = {
  264. &bin_attr_rw_nvmem,
  265. NULL,
  266. };
  267. static const struct attribute_group nvmem_bin_group = {
  268. .bin_attrs = nvmem_bin_attributes,
  269. .attrs = nvmem_attrs,
  270. .is_bin_visible = nvmem_bin_attr_is_visible,
  271. };
  272. static const struct attribute_group *nvmem_dev_groups[] = {
  273. &nvmem_bin_group,
  274. NULL,
  275. };
  276. static struct bin_attribute bin_attr_nvmem_eeprom_compat = {
  277. .attr = {
  278. .name = "eeprom",
  279. },
  280. .read = bin_attr_nvmem_read,
  281. .write = bin_attr_nvmem_write,
  282. };
  283. /*
  284. * nvmem_setup_compat() - Create an additional binary entry in
  285. * drivers sys directory, to be backwards compatible with the older
  286. * drivers/misc/eeprom drivers.
  287. */
  288. static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
  289. const struct nvmem_config *config)
  290. {
  291. int rval;
  292. if (!config->compat)
  293. return 0;
  294. if (!config->base_dev)
  295. return -EINVAL;
  296. if (config->type == NVMEM_TYPE_FRAM)
  297. bin_attr_nvmem_eeprom_compat.attr.name = "fram";
  298. nvmem->eeprom = bin_attr_nvmem_eeprom_compat;
  299. nvmem->eeprom.attr.mode = nvmem_bin_attr_get_umode(nvmem);
  300. nvmem->eeprom.size = nvmem->size;
  301. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  302. nvmem->eeprom.attr.key = &eeprom_lock_key;
  303. #endif
  304. nvmem->eeprom.private = &nvmem->dev;
  305. nvmem->base_dev = config->base_dev;
  306. rval = device_create_bin_file(nvmem->base_dev, &nvmem->eeprom);
  307. if (rval) {
  308. dev_err(&nvmem->dev,
  309. "Failed to create eeprom binary file %d\n", rval);
  310. return rval;
  311. }
  312. nvmem->flags |= FLAG_COMPAT;
  313. return 0;
  314. }
  315. static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
  316. const struct nvmem_config *config)
  317. {
  318. if (config->compat)
  319. device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
  320. }
  321. #else /* CONFIG_NVMEM_SYSFS */
  322. static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
  323. const struct nvmem_config *config)
  324. {
  325. return -ENOSYS;
  326. }
  327. static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
  328. const struct nvmem_config *config)
  329. {
  330. }
  331. #endif /* CONFIG_NVMEM_SYSFS */
  332. static void nvmem_release(struct device *dev)
  333. {
  334. struct nvmem_device *nvmem = to_nvmem_device(dev);
  335. ida_free(&nvmem_ida, nvmem->id);
  336. gpiod_put(nvmem->wp_gpio);
  337. kfree(nvmem);
  338. }
  339. static const struct device_type nvmem_provider_type = {
  340. .release = nvmem_release,
  341. };
  342. static struct bus_type nvmem_bus_type = {
  343. .name = "nvmem",
  344. };
  345. static void nvmem_cell_entry_drop(struct nvmem_cell_entry *cell)
  346. {
  347. blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell);
  348. mutex_lock(&nvmem_mutex);
  349. list_del(&cell->node);
  350. mutex_unlock(&nvmem_mutex);
  351. of_node_put(cell->np);
  352. kfree_const(cell->name);
  353. kfree(cell);
  354. }
  355. static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem)
  356. {
  357. struct nvmem_cell_entry *cell, *p;
  358. list_for_each_entry_safe(cell, p, &nvmem->cells, node)
  359. nvmem_cell_entry_drop(cell);
  360. }
  361. static void nvmem_cell_entry_add(struct nvmem_cell_entry *cell)
  362. {
  363. mutex_lock(&nvmem_mutex);
  364. list_add_tail(&cell->node, &cell->nvmem->cells);
  365. mutex_unlock(&nvmem_mutex);
  366. blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell);
  367. }
  368. static int nvmem_cell_info_to_nvmem_cell_entry_nodup(struct nvmem_device *nvmem,
  369. const struct nvmem_cell_info *info,
  370. struct nvmem_cell_entry *cell)
  371. {
  372. cell->nvmem = nvmem;
  373. cell->offset = info->offset;
  374. cell->bytes = info->bytes;
  375. cell->name = info->name;
  376. cell->bit_offset = info->bit_offset;
  377. cell->nbits = info->nbits;
  378. cell->np = info->np;
  379. if (cell->nbits)
  380. cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset,
  381. BITS_PER_BYTE);
  382. if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
  383. dev_err(&nvmem->dev,
  384. "cell %s unaligned to nvmem stride %d\n",
  385. cell->name ?: "<unknown>", nvmem->stride);
  386. return -EINVAL;
  387. }
  388. return 0;
  389. }
  390. static int nvmem_cell_info_to_nvmem_cell_entry(struct nvmem_device *nvmem,
  391. const struct nvmem_cell_info *info,
  392. struct nvmem_cell_entry *cell)
  393. {
  394. int err;
  395. err = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, cell);
  396. if (err)
  397. return err;
  398. cell->name = kstrdup_const(info->name, GFP_KERNEL);
  399. if (!cell->name)
  400. return -ENOMEM;
  401. return 0;
  402. }
  403. /**
  404. * nvmem_add_cells() - Add cell information to an nvmem device
  405. *
  406. * @nvmem: nvmem device to add cells to.
  407. * @info: nvmem cell info to add to the device
  408. * @ncells: number of cells in info
  409. *
  410. * Return: 0 or negative error code on failure.
  411. */
  412. static int nvmem_add_cells(struct nvmem_device *nvmem,
  413. const struct nvmem_cell_info *info,
  414. int ncells)
  415. {
  416. struct nvmem_cell_entry **cells;
  417. int i, rval;
  418. cells = kcalloc(ncells, sizeof(*cells), GFP_KERNEL);
  419. if (!cells)
  420. return -ENOMEM;
  421. for (i = 0; i < ncells; i++) {
  422. cells[i] = kzalloc(sizeof(**cells), GFP_KERNEL);
  423. if (!cells[i]) {
  424. rval = -ENOMEM;
  425. goto err;
  426. }
  427. rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, &info[i], cells[i]);
  428. if (rval) {
  429. kfree(cells[i]);
  430. goto err;
  431. }
  432. nvmem_cell_entry_add(cells[i]);
  433. }
  434. /* remove tmp array */
  435. kfree(cells);
  436. return 0;
  437. err:
  438. while (i--)
  439. nvmem_cell_entry_drop(cells[i]);
  440. kfree(cells);
  441. return rval;
  442. }
  443. /**
  444. * nvmem_register_notifier() - Register a notifier block for nvmem events.
  445. *
  446. * @nb: notifier block to be called on nvmem events.
  447. *
  448. * Return: 0 on success, negative error number on failure.
  449. */
  450. int nvmem_register_notifier(struct notifier_block *nb)
  451. {
  452. return blocking_notifier_chain_register(&nvmem_notifier, nb);
  453. }
  454. EXPORT_SYMBOL_GPL(nvmem_register_notifier);
  455. /**
  456. * nvmem_unregister_notifier() - Unregister a notifier block for nvmem events.
  457. *
  458. * @nb: notifier block to be unregistered.
  459. *
  460. * Return: 0 on success, negative error number on failure.
  461. */
  462. int nvmem_unregister_notifier(struct notifier_block *nb)
  463. {
  464. return blocking_notifier_chain_unregister(&nvmem_notifier, nb);
  465. }
  466. EXPORT_SYMBOL_GPL(nvmem_unregister_notifier);
  467. static int nvmem_add_cells_from_table(struct nvmem_device *nvmem)
  468. {
  469. const struct nvmem_cell_info *info;
  470. struct nvmem_cell_table *table;
  471. struct nvmem_cell_entry *cell;
  472. int rval = 0, i;
  473. mutex_lock(&nvmem_cell_mutex);
  474. list_for_each_entry(table, &nvmem_cell_tables, node) {
  475. if (strcmp(nvmem_dev_name(nvmem), table->nvmem_name) == 0) {
  476. for (i = 0; i < table->ncells; i++) {
  477. info = &table->cells[i];
  478. cell = kzalloc(sizeof(*cell), GFP_KERNEL);
  479. if (!cell) {
  480. rval = -ENOMEM;
  481. goto out;
  482. }
  483. rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, info, cell);
  484. if (rval) {
  485. kfree(cell);
  486. goto out;
  487. }
  488. nvmem_cell_entry_add(cell);
  489. }
  490. }
  491. }
  492. out:
  493. mutex_unlock(&nvmem_cell_mutex);
  494. return rval;
  495. }
  496. static struct nvmem_cell_entry *
  497. nvmem_find_cell_entry_by_name(struct nvmem_device *nvmem, const char *cell_id)
  498. {
  499. struct nvmem_cell_entry *iter, *cell = NULL;
  500. mutex_lock(&nvmem_mutex);
  501. list_for_each_entry(iter, &nvmem->cells, node) {
  502. if (strcmp(cell_id, iter->name) == 0) {
  503. cell = iter;
  504. break;
  505. }
  506. }
  507. mutex_unlock(&nvmem_mutex);
  508. return cell;
  509. }
  510. static int nvmem_validate_keepouts(struct nvmem_device *nvmem)
  511. {
  512. unsigned int cur = 0;
  513. const struct nvmem_keepout *keepout = nvmem->keepout;
  514. const struct nvmem_keepout *keepoutend = keepout + nvmem->nkeepout;
  515. while (keepout < keepoutend) {
  516. /* Ensure keepouts are sorted and don't overlap. */
  517. if (keepout->start < cur) {
  518. dev_err(&nvmem->dev,
  519. "Keepout regions aren't sorted or overlap.\n");
  520. return -ERANGE;
  521. }
  522. if (keepout->end < keepout->start) {
  523. dev_err(&nvmem->dev,
  524. "Invalid keepout region.\n");
  525. return -EINVAL;
  526. }
  527. /*
  528. * Validate keepouts (and holes between) don't violate
  529. * word_size constraints.
  530. */
  531. if ((keepout->end - keepout->start < nvmem->word_size) ||
  532. ((keepout->start != cur) &&
  533. (keepout->start - cur < nvmem->word_size))) {
  534. dev_err(&nvmem->dev,
  535. "Keepout regions violate word_size constraints.\n");
  536. return -ERANGE;
  537. }
  538. /* Validate keepouts don't violate stride (alignment). */
  539. if (!IS_ALIGNED(keepout->start, nvmem->stride) ||
  540. !IS_ALIGNED(keepout->end, nvmem->stride)) {
  541. dev_err(&nvmem->dev,
  542. "Keepout regions violate stride.\n");
  543. return -EINVAL;
  544. }
  545. cur = keepout->end;
  546. keepout++;
  547. }
  548. return 0;
  549. }
  550. static int nvmem_add_cells_from_of(struct nvmem_device *nvmem)
  551. {
  552. struct device_node *parent, *child;
  553. struct device *dev = &nvmem->dev;
  554. struct nvmem_cell_entry *cell;
  555. const __be32 *addr;
  556. int len;
  557. parent = dev->of_node;
  558. for_each_child_of_node(parent, child) {
  559. addr = of_get_property(child, "reg", &len);
  560. if (!addr)
  561. continue;
  562. if (len < 2 * sizeof(u32)) {
  563. dev_err(dev, "nvmem: invalid reg on %pOF\n", child);
  564. of_node_put(child);
  565. return -EINVAL;
  566. }
  567. cell = kzalloc(sizeof(*cell), GFP_KERNEL);
  568. if (!cell) {
  569. of_node_put(child);
  570. return -ENOMEM;
  571. }
  572. cell->nvmem = nvmem;
  573. cell->offset = be32_to_cpup(addr++);
  574. cell->bytes = be32_to_cpup(addr);
  575. cell->name = kasprintf(GFP_KERNEL, "%pOFn", child);
  576. addr = of_get_property(child, "bits", &len);
  577. if (addr && len == (2 * sizeof(u32))) {
  578. cell->bit_offset = be32_to_cpup(addr++);
  579. cell->nbits = be32_to_cpup(addr);
  580. }
  581. if (cell->nbits)
  582. cell->bytes = DIV_ROUND_UP(
  583. cell->nbits + cell->bit_offset,
  584. BITS_PER_BYTE);
  585. if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
  586. dev_err(dev, "cell %s unaligned to nvmem stride %d\n",
  587. cell->name, nvmem->stride);
  588. /* Cells already added will be freed later. */
  589. kfree_const(cell->name);
  590. kfree(cell);
  591. of_node_put(child);
  592. return -EINVAL;
  593. }
  594. cell->np = of_node_get(child);
  595. nvmem_cell_entry_add(cell);
  596. }
  597. return 0;
  598. }
  599. /**
  600. * nvmem_register() - Register a nvmem device for given nvmem_config.
  601. * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
  602. *
  603. * @config: nvmem device configuration with which nvmem device is created.
  604. *
  605. * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
  606. * on success.
  607. */
  608. struct nvmem_device *nvmem_register(const struct nvmem_config *config)
  609. {
  610. struct nvmem_device *nvmem;
  611. int rval;
  612. if (!config->dev)
  613. return ERR_PTR(-EINVAL);
  614. if (!config->reg_read && !config->reg_write)
  615. return ERR_PTR(-EINVAL);
  616. nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL);
  617. if (!nvmem)
  618. return ERR_PTR(-ENOMEM);
  619. rval = ida_alloc(&nvmem_ida, GFP_KERNEL);
  620. if (rval < 0) {
  621. kfree(nvmem);
  622. return ERR_PTR(rval);
  623. }
  624. nvmem->id = rval;
  625. nvmem->dev.type = &nvmem_provider_type;
  626. nvmem->dev.bus = &nvmem_bus_type;
  627. nvmem->dev.parent = config->dev;
  628. device_initialize(&nvmem->dev);
  629. if (!config->ignore_wp)
  630. nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
  631. GPIOD_OUT_HIGH);
  632. if (IS_ERR(nvmem->wp_gpio)) {
  633. rval = PTR_ERR(nvmem->wp_gpio);
  634. nvmem->wp_gpio = NULL;
  635. goto err_put_device;
  636. }
  637. kref_init(&nvmem->refcnt);
  638. INIT_LIST_HEAD(&nvmem->cells);
  639. nvmem->owner = config->owner;
  640. if (!nvmem->owner && config->dev->driver)
  641. nvmem->owner = config->dev->driver->owner;
  642. nvmem->stride = config->stride ?: 1;
  643. nvmem->word_size = config->word_size ?: 1;
  644. nvmem->size = config->size;
  645. nvmem->root_only = config->root_only;
  646. nvmem->priv = config->priv;
  647. nvmem->type = config->type;
  648. nvmem->reg_read = config->reg_read;
  649. nvmem->reg_write = config->reg_write;
  650. nvmem->cell_post_process = config->cell_post_process;
  651. nvmem->keepout = config->keepout;
  652. nvmem->nkeepout = config->nkeepout;
  653. if (config->of_node)
  654. nvmem->dev.of_node = config->of_node;
  655. else if (!config->no_of_node)
  656. nvmem->dev.of_node = config->dev->of_node;
  657. switch (config->id) {
  658. case NVMEM_DEVID_NONE:
  659. rval = dev_set_name(&nvmem->dev, "%s", config->name);
  660. break;
  661. case NVMEM_DEVID_AUTO:
  662. rval = dev_set_name(&nvmem->dev, "%s%d", config->name, nvmem->id);
  663. break;
  664. default:
  665. rval = dev_set_name(&nvmem->dev, "%s%d",
  666. config->name ? : "nvmem",
  667. config->name ? config->id : nvmem->id);
  668. break;
  669. }
  670. if (rval)
  671. goto err_put_device;
  672. nvmem->read_only = device_property_present(config->dev, "read-only") ||
  673. config->read_only || !nvmem->reg_write;
  674. #ifdef CONFIG_NVMEM_SYSFS
  675. nvmem->dev.groups = nvmem_dev_groups;
  676. #endif
  677. if (nvmem->nkeepout) {
  678. rval = nvmem_validate_keepouts(nvmem);
  679. if (rval)
  680. goto err_put_device;
  681. }
  682. if (config->compat) {
  683. rval = nvmem_sysfs_setup_compat(nvmem, config);
  684. if (rval)
  685. goto err_put_device;
  686. }
  687. if (config->cells) {
  688. rval = nvmem_add_cells(nvmem, config->cells, config->ncells);
  689. if (rval)
  690. goto err_remove_cells;
  691. }
  692. rval = nvmem_add_cells_from_table(nvmem);
  693. if (rval)
  694. goto err_remove_cells;
  695. rval = nvmem_add_cells_from_of(nvmem);
  696. if (rval)
  697. goto err_remove_cells;
  698. dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
  699. rval = device_add(&nvmem->dev);
  700. if (rval)
  701. goto err_remove_cells;
  702. blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
  703. return nvmem;
  704. err_remove_cells:
  705. nvmem_device_remove_all_cells(nvmem);
  706. if (config->compat)
  707. nvmem_sysfs_remove_compat(nvmem, config);
  708. err_put_device:
  709. put_device(&nvmem->dev);
  710. return ERR_PTR(rval);
  711. }
  712. EXPORT_SYMBOL_GPL(nvmem_register);
  713. static void nvmem_device_release(struct kref *kref)
  714. {
  715. struct nvmem_device *nvmem;
  716. nvmem = container_of(kref, struct nvmem_device, refcnt);
  717. blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
  718. if (nvmem->flags & FLAG_COMPAT)
  719. device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
  720. nvmem_device_remove_all_cells(nvmem);
  721. device_unregister(&nvmem->dev);
  722. }
  723. /**
  724. * nvmem_unregister() - Unregister previously registered nvmem device
  725. *
  726. * @nvmem: Pointer to previously registered nvmem device.
  727. */
  728. void nvmem_unregister(struct nvmem_device *nvmem)
  729. {
  730. if (nvmem)
  731. kref_put(&nvmem->refcnt, nvmem_device_release);
  732. }
  733. EXPORT_SYMBOL_GPL(nvmem_unregister);
  734. static void devm_nvmem_unregister(void *nvmem)
  735. {
  736. nvmem_unregister(nvmem);
  737. }
  738. /**
  739. * devm_nvmem_register() - Register a managed nvmem device for given
  740. * nvmem_config.
  741. * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
  742. *
  743. * @dev: Device that uses the nvmem device.
  744. * @config: nvmem device configuration with which nvmem device is created.
  745. *
  746. * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
  747. * on success.
  748. */
  749. struct nvmem_device *devm_nvmem_register(struct device *dev,
  750. const struct nvmem_config *config)
  751. {
  752. struct nvmem_device *nvmem;
  753. int ret;
  754. nvmem = nvmem_register(config);
  755. if (IS_ERR(nvmem))
  756. return nvmem;
  757. ret = devm_add_action_or_reset(dev, devm_nvmem_unregister, nvmem);
  758. if (ret)
  759. return ERR_PTR(ret);
  760. return nvmem;
  761. }
  762. EXPORT_SYMBOL_GPL(devm_nvmem_register);
  763. static struct nvmem_device *__nvmem_device_get(void *data,
  764. int (*match)(struct device *dev, const void *data))
  765. {
  766. struct nvmem_device *nvmem = NULL;
  767. struct device *dev;
  768. mutex_lock(&nvmem_mutex);
  769. dev = bus_find_device(&nvmem_bus_type, NULL, data, match);
  770. if (dev)
  771. nvmem = to_nvmem_device(dev);
  772. mutex_unlock(&nvmem_mutex);
  773. if (!nvmem)
  774. return ERR_PTR(-EPROBE_DEFER);
  775. if (!try_module_get(nvmem->owner)) {
  776. dev_err(&nvmem->dev,
  777. "could not increase module refcount for cell %s\n",
  778. nvmem_dev_name(nvmem));
  779. put_device(&nvmem->dev);
  780. return ERR_PTR(-EINVAL);
  781. }
  782. kref_get(&nvmem->refcnt);
  783. return nvmem;
  784. }
  785. static void __nvmem_device_put(struct nvmem_device *nvmem)
  786. {
  787. put_device(&nvmem->dev);
  788. module_put(nvmem->owner);
  789. kref_put(&nvmem->refcnt, nvmem_device_release);
  790. }
  791. #if IS_ENABLED(CONFIG_OF)
  792. /**
  793. * of_nvmem_device_get() - Get nvmem device from a given id
  794. *
  795. * @np: Device tree node that uses the nvmem device.
  796. * @id: nvmem name from nvmem-names property.
  797. *
  798. * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
  799. * on success.
  800. */
  801. struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id)
  802. {
  803. struct device_node *nvmem_np;
  804. struct nvmem_device *nvmem;
  805. int index = 0;
  806. if (id)
  807. index = of_property_match_string(np, "nvmem-names", id);
  808. nvmem_np = of_parse_phandle(np, "nvmem", index);
  809. if (!nvmem_np)
  810. return ERR_PTR(-ENOENT);
  811. nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
  812. of_node_put(nvmem_np);
  813. return nvmem;
  814. }
  815. EXPORT_SYMBOL_GPL(of_nvmem_device_get);
  816. #endif
  817. /**
  818. * nvmem_device_get() - Get nvmem device from a given id
  819. *
  820. * @dev: Device that uses the nvmem device.
  821. * @dev_name: name of the requested nvmem device.
  822. *
  823. * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
  824. * on success.
  825. */
  826. struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name)
  827. {
  828. if (dev->of_node) { /* try dt first */
  829. struct nvmem_device *nvmem;
  830. nvmem = of_nvmem_device_get(dev->of_node, dev_name);
  831. if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER)
  832. return nvmem;
  833. }
  834. return __nvmem_device_get((void *)dev_name, device_match_name);
  835. }
  836. EXPORT_SYMBOL_GPL(nvmem_device_get);
  837. /**
  838. * nvmem_device_find() - Find nvmem device with matching function
  839. *
  840. * @data: Data to pass to match function
  841. * @match: Callback function to check device
  842. *
  843. * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
  844. * on success.
  845. */
  846. struct nvmem_device *nvmem_device_find(void *data,
  847. int (*match)(struct device *dev, const void *data))
  848. {
  849. return __nvmem_device_get(data, match);
  850. }
  851. EXPORT_SYMBOL_GPL(nvmem_device_find);
  852. static int devm_nvmem_device_match(struct device *dev, void *res, void *data)
  853. {
  854. struct nvmem_device **nvmem = res;
  855. if (WARN_ON(!nvmem || !*nvmem))
  856. return 0;
  857. return *nvmem == data;
  858. }
  859. static void devm_nvmem_device_release(struct device *dev, void *res)
  860. {
  861. nvmem_device_put(*(struct nvmem_device **)res);
  862. }
  863. /**
  864. * devm_nvmem_device_put() - put alredy got nvmem device
  865. *
  866. * @dev: Device that uses the nvmem device.
  867. * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
  868. * that needs to be released.
  869. */
  870. void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)
  871. {
  872. int ret;
  873. ret = devres_release(dev, devm_nvmem_device_release,
  874. devm_nvmem_device_match, nvmem);
  875. WARN_ON(ret);
  876. }
  877. EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
  878. /**
  879. * nvmem_device_put() - put alredy got nvmem device
  880. *
  881. * @nvmem: pointer to nvmem device that needs to be released.
  882. */
  883. void nvmem_device_put(struct nvmem_device *nvmem)
  884. {
  885. __nvmem_device_put(nvmem);
  886. }
  887. EXPORT_SYMBOL_GPL(nvmem_device_put);
  888. /**
  889. * devm_nvmem_device_get() - Get nvmem cell of device form a given id
  890. *
  891. * @dev: Device that requests the nvmem device.
  892. * @id: name id for the requested nvmem device.
  893. *
  894. * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell
  895. * on success. The nvmem_cell will be freed by the automatically once the
  896. * device is freed.
  897. */
  898. struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id)
  899. {
  900. struct nvmem_device **ptr, *nvmem;
  901. ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL);
  902. if (!ptr)
  903. return ERR_PTR(-ENOMEM);
  904. nvmem = nvmem_device_get(dev, id);
  905. if (!IS_ERR(nvmem)) {
  906. *ptr = nvmem;
  907. devres_add(dev, ptr);
  908. } else {
  909. devres_free(ptr);
  910. }
  911. return nvmem;
  912. }
  913. EXPORT_SYMBOL_GPL(devm_nvmem_device_get);
  914. static struct nvmem_cell *nvmem_create_cell(struct nvmem_cell_entry *entry, const char *id)
  915. {
  916. struct nvmem_cell *cell;
  917. const char *name = NULL;
  918. cell = kzalloc(sizeof(*cell), GFP_KERNEL);
  919. if (!cell)
  920. return ERR_PTR(-ENOMEM);
  921. if (id) {
  922. name = kstrdup_const(id, GFP_KERNEL);
  923. if (!name) {
  924. kfree(cell);
  925. return ERR_PTR(-ENOMEM);
  926. }
  927. }
  928. cell->id = name;
  929. cell->entry = entry;
  930. return cell;
  931. }
  932. static struct nvmem_cell *
  933. nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
  934. {
  935. struct nvmem_cell_entry *cell_entry;
  936. struct nvmem_cell *cell = ERR_PTR(-ENOENT);
  937. struct nvmem_cell_lookup *lookup;
  938. struct nvmem_device *nvmem;
  939. const char *dev_id;
  940. if (!dev)
  941. return ERR_PTR(-EINVAL);
  942. dev_id = dev_name(dev);
  943. mutex_lock(&nvmem_lookup_mutex);
  944. list_for_each_entry(lookup, &nvmem_lookup_list, node) {
  945. if ((strcmp(lookup->dev_id, dev_id) == 0) &&
  946. (strcmp(lookup->con_id, con_id) == 0)) {
  947. /* This is the right entry. */
  948. nvmem = __nvmem_device_get((void *)lookup->nvmem_name,
  949. device_match_name);
  950. if (IS_ERR(nvmem)) {
  951. /* Provider may not be registered yet. */
  952. cell = ERR_CAST(nvmem);
  953. break;
  954. }
  955. cell_entry = nvmem_find_cell_entry_by_name(nvmem,
  956. lookup->cell_name);
  957. if (!cell_entry) {
  958. __nvmem_device_put(nvmem);
  959. cell = ERR_PTR(-ENOENT);
  960. } else {
  961. cell = nvmem_create_cell(cell_entry, con_id);
  962. if (IS_ERR(cell))
  963. __nvmem_device_put(nvmem);
  964. }
  965. break;
  966. }
  967. }
  968. mutex_unlock(&nvmem_lookup_mutex);
  969. return cell;
  970. }
  971. #if IS_ENABLED(CONFIG_OF)
  972. static struct nvmem_cell_entry *
  973. nvmem_find_cell_entry_by_node(struct nvmem_device *nvmem, struct device_node *np)
  974. {
  975. struct nvmem_cell_entry *iter, *cell = NULL;
  976. mutex_lock(&nvmem_mutex);
  977. list_for_each_entry(iter, &nvmem->cells, node) {
  978. if (np == iter->np) {
  979. cell = iter;
  980. break;
  981. }
  982. }
  983. mutex_unlock(&nvmem_mutex);
  984. return cell;
  985. }
  986. /**
  987. * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
  988. *
  989. * @np: Device tree node that uses the nvmem cell.
  990. * @id: nvmem cell name from nvmem-cell-names property, or NULL
  991. * for the cell at index 0 (the lone cell with no accompanying
  992. * nvmem-cell-names property).
  993. *
  994. * Return: Will be an ERR_PTR() on error or a valid pointer
  995. * to a struct nvmem_cell. The nvmem_cell will be freed by the
  996. * nvmem_cell_put().
  997. */
  998. struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
  999. {
  1000. struct device_node *cell_np, *nvmem_np;
  1001. struct nvmem_device *nvmem;
  1002. struct nvmem_cell_entry *cell_entry;
  1003. struct nvmem_cell *cell;
  1004. int index = 0;
  1005. /* if cell name exists, find index to the name */
  1006. if (id)
  1007. index = of_property_match_string(np, "nvmem-cell-names", id);
  1008. cell_np = of_parse_phandle(np, "nvmem-cells", index);
  1009. if (!cell_np)
  1010. return ERR_PTR(-ENOENT);
  1011. nvmem_np = of_get_parent(cell_np);
  1012. if (!nvmem_np) {
  1013. of_node_put(cell_np);
  1014. return ERR_PTR(-EINVAL);
  1015. }
  1016. nvmem = __nvmem_device_get(nvmem_np, device_match_of_node);
  1017. of_node_put(nvmem_np);
  1018. if (IS_ERR(nvmem)) {
  1019. of_node_put(cell_np);
  1020. return ERR_CAST(nvmem);
  1021. }
  1022. cell_entry = nvmem_find_cell_entry_by_node(nvmem, cell_np);
  1023. of_node_put(cell_np);
  1024. if (!cell_entry) {
  1025. __nvmem_device_put(nvmem);
  1026. return ERR_PTR(-ENOENT);
  1027. }
  1028. cell = nvmem_create_cell(cell_entry, id);
  1029. if (IS_ERR(cell))
  1030. __nvmem_device_put(nvmem);
  1031. return cell;
  1032. }
  1033. EXPORT_SYMBOL_GPL(of_nvmem_cell_get);
  1034. #endif
  1035. /**
  1036. * nvmem_cell_get() - Get nvmem cell of device form a given cell name
  1037. *
  1038. * @dev: Device that requests the nvmem cell.
  1039. * @id: nvmem cell name to get (this corresponds with the name from the
  1040. * nvmem-cell-names property for DT systems and with the con_id from
  1041. * the lookup entry for non-DT systems).
  1042. *
  1043. * Return: Will be an ERR_PTR() on error or a valid pointer
  1044. * to a struct nvmem_cell. The nvmem_cell will be freed by the
  1045. * nvmem_cell_put().
  1046. */
  1047. struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *id)
  1048. {
  1049. struct nvmem_cell *cell;
  1050. if (dev->of_node) { /* try dt first */
  1051. cell = of_nvmem_cell_get(dev->of_node, id);
  1052. if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER)
  1053. return cell;
  1054. }
  1055. /* NULL cell id only allowed for device tree; invalid otherwise */
  1056. if (!id)
  1057. return ERR_PTR(-EINVAL);
  1058. return nvmem_cell_get_from_lookup(dev, id);
  1059. }
  1060. EXPORT_SYMBOL_GPL(nvmem_cell_get);
  1061. static void devm_nvmem_cell_release(struct device *dev, void *res)
  1062. {
  1063. nvmem_cell_put(*(struct nvmem_cell **)res);
  1064. }
  1065. /**
  1066. * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
  1067. *
  1068. * @dev: Device that requests the nvmem cell.
  1069. * @id: nvmem cell name id to get.
  1070. *
  1071. * Return: Will be an ERR_PTR() on error or a valid pointer
  1072. * to a struct nvmem_cell. The nvmem_cell will be freed by the
  1073. * automatically once the device is freed.
  1074. */
  1075. struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id)
  1076. {
  1077. struct nvmem_cell **ptr, *cell;
  1078. ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL);
  1079. if (!ptr)
  1080. return ERR_PTR(-ENOMEM);
  1081. cell = nvmem_cell_get(dev, id);
  1082. if (!IS_ERR(cell)) {
  1083. *ptr = cell;
  1084. devres_add(dev, ptr);
  1085. } else {
  1086. devres_free(ptr);
  1087. }
  1088. return cell;
  1089. }
  1090. EXPORT_SYMBOL_GPL(devm_nvmem_cell_get);
  1091. static int devm_nvmem_cell_match(struct device *dev, void *res, void *data)
  1092. {
  1093. struct nvmem_cell **c = res;
  1094. if (WARN_ON(!c || !*c))
  1095. return 0;
  1096. return *c == data;
  1097. }
  1098. /**
  1099. * devm_nvmem_cell_put() - Release previously allocated nvmem cell
  1100. * from devm_nvmem_cell_get.
  1101. *
  1102. * @dev: Device that requests the nvmem cell.
  1103. * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
  1104. */
  1105. void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell)
  1106. {
  1107. int ret;
  1108. ret = devres_release(dev, devm_nvmem_cell_release,
  1109. devm_nvmem_cell_match, cell);
  1110. WARN_ON(ret);
  1111. }
  1112. EXPORT_SYMBOL(devm_nvmem_cell_put);
  1113. /**
  1114. * nvmem_cell_put() - Release previously allocated nvmem cell.
  1115. *
  1116. * @cell: Previously allocated nvmem cell by nvmem_cell_get().
  1117. */
  1118. void nvmem_cell_put(struct nvmem_cell *cell)
  1119. {
  1120. struct nvmem_device *nvmem = cell->entry->nvmem;
  1121. if (cell->id)
  1122. kfree_const(cell->id);
  1123. kfree(cell);
  1124. __nvmem_device_put(nvmem);
  1125. }
  1126. EXPORT_SYMBOL_GPL(nvmem_cell_put);
  1127. static void nvmem_shift_read_buffer_in_place(struct nvmem_cell_entry *cell, void *buf)
  1128. {
  1129. u8 *p, *b;
  1130. int i, extra, bit_offset = cell->bit_offset;
  1131. p = b = buf;
  1132. if (bit_offset) {
  1133. /* First shift */
  1134. *b++ >>= bit_offset;
  1135. /* setup rest of the bytes if any */
  1136. for (i = 1; i < cell->bytes; i++) {
  1137. /* Get bits from next byte and shift them towards msb */
  1138. *p |= *b << (BITS_PER_BYTE - bit_offset);
  1139. p = b;
  1140. *b++ >>= bit_offset;
  1141. }
  1142. } else {
  1143. /* point to the msb */
  1144. p += cell->bytes - 1;
  1145. }
  1146. /* result fits in less bytes */
  1147. extra = cell->bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE);
  1148. while (--extra >= 0)
  1149. *p-- = 0;
  1150. /* clear msb bits if any leftover in the last byte */
  1151. if (cell->nbits % BITS_PER_BYTE)
  1152. *p &= GENMASK((cell->nbits % BITS_PER_BYTE) - 1, 0);
  1153. }
  1154. static int __nvmem_cell_read(struct nvmem_device *nvmem,
  1155. struct nvmem_cell_entry *cell,
  1156. void *buf, size_t *len, const char *id)
  1157. {
  1158. int rc;
  1159. rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->bytes);
  1160. if (rc)
  1161. return rc;
  1162. /* shift bits in-place */
  1163. if (cell->bit_offset || cell->nbits)
  1164. nvmem_shift_read_buffer_in_place(cell, buf);
  1165. if (nvmem->cell_post_process) {
  1166. rc = nvmem->cell_post_process(nvmem->priv, id,
  1167. cell->offset, buf, cell->bytes);
  1168. if (rc)
  1169. return rc;
  1170. }
  1171. if (len)
  1172. *len = cell->bytes;
  1173. return 0;
  1174. }
  1175. /**
  1176. * nvmem_cell_read() - Read a given nvmem cell
  1177. *
  1178. * @cell: nvmem cell to be read.
  1179. * @len: pointer to length of cell which will be populated on successful read;
  1180. * can be NULL.
  1181. *
  1182. * Return: ERR_PTR() on error or a valid pointer to a buffer on success. The
  1183. * buffer should be freed by the consumer with a kfree().
  1184. */
  1185. void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
  1186. {
  1187. struct nvmem_device *nvmem = cell->entry->nvmem;
  1188. u8 *buf;
  1189. int rc;
  1190. if (!nvmem)
  1191. return ERR_PTR(-EINVAL);
  1192. buf = kzalloc(cell->entry->bytes, GFP_KERNEL);
  1193. if (!buf)
  1194. return ERR_PTR(-ENOMEM);
  1195. rc = __nvmem_cell_read(nvmem, cell->entry, buf, len, cell->id);
  1196. if (rc) {
  1197. kfree(buf);
  1198. return ERR_PTR(rc);
  1199. }
  1200. return buf;
  1201. }
  1202. EXPORT_SYMBOL_GPL(nvmem_cell_read);
  1203. static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell_entry *cell,
  1204. u8 *_buf, int len)
  1205. {
  1206. struct nvmem_device *nvmem = cell->nvmem;
  1207. int i, rc, nbits, bit_offset = cell->bit_offset;
  1208. u8 v, *p, *buf, *b, pbyte, pbits;
  1209. nbits = cell->nbits;
  1210. buf = kzalloc(cell->bytes, GFP_KERNEL);
  1211. if (!buf)
  1212. return ERR_PTR(-ENOMEM);
  1213. memcpy(buf, _buf, len);
  1214. p = b = buf;
  1215. if (bit_offset) {
  1216. pbyte = *b;
  1217. *b <<= bit_offset;
  1218. /* setup the first byte with lsb bits from nvmem */
  1219. rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
  1220. if (rc)
  1221. goto err;
  1222. *b++ |= GENMASK(bit_offset - 1, 0) & v;
  1223. /* setup rest of the byte if any */
  1224. for (i = 1; i < cell->bytes; i++) {
  1225. /* Get last byte bits and shift them towards lsb */
  1226. pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset);
  1227. pbyte = *b;
  1228. p = b;
  1229. *b <<= bit_offset;
  1230. *b++ |= pbits;
  1231. }
  1232. }
  1233. /* if it's not end on byte boundary */
  1234. if ((nbits + bit_offset) % BITS_PER_BYTE) {
  1235. /* setup the last byte with msb bits from nvmem */
  1236. rc = nvmem_reg_read(nvmem,
  1237. cell->offset + cell->bytes - 1, &v, 1);
  1238. if (rc)
  1239. goto err;
  1240. *p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
  1241. }
  1242. return buf;
  1243. err:
  1244. kfree(buf);
  1245. return ERR_PTR(rc);
  1246. }
  1247. static int __nvmem_cell_entry_write(struct nvmem_cell_entry *cell, void *buf, size_t len)
  1248. {
  1249. struct nvmem_device *nvmem = cell->nvmem;
  1250. int rc;
  1251. if (!nvmem || nvmem->read_only ||
  1252. (cell->bit_offset == 0 && len != cell->bytes))
  1253. return -EINVAL;
  1254. if (cell->bit_offset || cell->nbits) {
  1255. buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
  1256. if (IS_ERR(buf))
  1257. return PTR_ERR(buf);
  1258. }
  1259. rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
  1260. /* free the tmp buffer */
  1261. if (cell->bit_offset || cell->nbits)
  1262. kfree(buf);
  1263. if (rc)
  1264. return rc;
  1265. return len;
  1266. }
  1267. /**
  1268. * nvmem_cell_write() - Write to a given nvmem cell
  1269. *
  1270. * @cell: nvmem cell to be written.
  1271. * @buf: Buffer to be written.
  1272. * @len: length of buffer to be written to nvmem cell.
  1273. *
  1274. * Return: length of bytes written or negative on failure.
  1275. */
  1276. int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
  1277. {
  1278. return __nvmem_cell_entry_write(cell->entry, buf, len);
  1279. }
  1280. EXPORT_SYMBOL_GPL(nvmem_cell_write);
  1281. static int nvmem_cell_read_common(struct device *dev, const char *cell_id,
  1282. void *val, size_t count)
  1283. {
  1284. struct nvmem_cell *cell;
  1285. void *buf;
  1286. size_t len;
  1287. cell = nvmem_cell_get(dev, cell_id);
  1288. if (IS_ERR(cell))
  1289. return PTR_ERR(cell);
  1290. buf = nvmem_cell_read(cell, &len);
  1291. if (IS_ERR(buf)) {
  1292. nvmem_cell_put(cell);
  1293. return PTR_ERR(buf);
  1294. }
  1295. if (len != count) {
  1296. kfree(buf);
  1297. nvmem_cell_put(cell);
  1298. return -EINVAL;
  1299. }
  1300. memcpy(val, buf, count);
  1301. kfree(buf);
  1302. nvmem_cell_put(cell);
  1303. return 0;
  1304. }
  1305. /**
  1306. * nvmem_cell_read_u8() - Read a cell value as a u8
  1307. *
  1308. * @dev: Device that requests the nvmem cell.
  1309. * @cell_id: Name of nvmem cell to read.
  1310. * @val: pointer to output value.
  1311. *
  1312. * Return: 0 on success or negative errno.
  1313. */
  1314. int nvmem_cell_read_u8(struct device *dev, const char *cell_id, u8 *val)
  1315. {
  1316. return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
  1317. }
  1318. EXPORT_SYMBOL_GPL(nvmem_cell_read_u8);
  1319. /**
  1320. * nvmem_cell_read_u16() - Read a cell value as a u16
  1321. *
  1322. * @dev: Device that requests the nvmem cell.
  1323. * @cell_id: Name of nvmem cell to read.
  1324. * @val: pointer to output value.
  1325. *
  1326. * Return: 0 on success or negative errno.
  1327. */
  1328. int nvmem_cell_read_u16(struct device *dev, const char *cell_id, u16 *val)
  1329. {
  1330. return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
  1331. }
  1332. EXPORT_SYMBOL_GPL(nvmem_cell_read_u16);
  1333. /**
  1334. * nvmem_cell_read_u32() - Read a cell value as a u32
  1335. *
  1336. * @dev: Device that requests the nvmem cell.
  1337. * @cell_id: Name of nvmem cell to read.
  1338. * @val: pointer to output value.
  1339. *
  1340. * Return: 0 on success or negative errno.
  1341. */
  1342. int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val)
  1343. {
  1344. return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
  1345. }
  1346. EXPORT_SYMBOL_GPL(nvmem_cell_read_u32);
  1347. /**
  1348. * nvmem_cell_read_u64() - Read a cell value as a u64
  1349. *
  1350. * @dev: Device that requests the nvmem cell.
  1351. * @cell_id: Name of nvmem cell to read.
  1352. * @val: pointer to output value.
  1353. *
  1354. * Return: 0 on success or negative errno.
  1355. */
  1356. int nvmem_cell_read_u64(struct device *dev, const char *cell_id, u64 *val)
  1357. {
  1358. return nvmem_cell_read_common(dev, cell_id, val, sizeof(*val));
  1359. }
  1360. EXPORT_SYMBOL_GPL(nvmem_cell_read_u64);
  1361. static const void *nvmem_cell_read_variable_common(struct device *dev,
  1362. const char *cell_id,
  1363. size_t max_len, size_t *len)
  1364. {
  1365. struct nvmem_cell *cell;
  1366. int nbits;
  1367. void *buf;
  1368. cell = nvmem_cell_get(dev, cell_id);
  1369. if (IS_ERR(cell))
  1370. return cell;
  1371. nbits = cell->entry->nbits;
  1372. buf = nvmem_cell_read(cell, len);
  1373. nvmem_cell_put(cell);
  1374. if (IS_ERR(buf))
  1375. return buf;
  1376. /*
  1377. * If nbits is set then nvmem_cell_read() can significantly exaggerate
  1378. * the length of the real data. Throw away the extra junk.
  1379. */
  1380. if (nbits)
  1381. *len = DIV_ROUND_UP(nbits, 8);
  1382. if (*len > max_len) {
  1383. kfree(buf);
  1384. return ERR_PTR(-ERANGE);
  1385. }
  1386. return buf;
  1387. }
  1388. /**
  1389. * nvmem_cell_read_variable_le_u32() - Read up to 32-bits of data as a little endian number.
  1390. *
  1391. * @dev: Device that requests the nvmem cell.
  1392. * @cell_id: Name of nvmem cell to read.
  1393. * @val: pointer to output value.
  1394. *
  1395. * Return: 0 on success or negative errno.
  1396. */
  1397. int nvmem_cell_read_variable_le_u32(struct device *dev, const char *cell_id,
  1398. u32 *val)
  1399. {
  1400. size_t len;
  1401. const u8 *buf;
  1402. int i;
  1403. buf = nvmem_cell_read_variable_common(dev, cell_id, sizeof(*val), &len);
  1404. if (IS_ERR(buf))
  1405. return PTR_ERR(buf);
  1406. /* Copy w/ implicit endian conversion */
  1407. *val = 0;
  1408. for (i = 0; i < len; i++)
  1409. *val |= buf[i] << (8 * i);
  1410. kfree(buf);
  1411. return 0;
  1412. }
  1413. EXPORT_SYMBOL_GPL(nvmem_cell_read_variable_le_u32);
  1414. /**
  1415. * nvmem_cell_read_variable_le_u64() - Read up to 64-bits of data as a little endian number.
  1416. *
  1417. * @dev: Device that requests the nvmem cell.
  1418. * @cell_id: Name of nvmem cell to read.
  1419. * @val: pointer to output value.
  1420. *
  1421. * Return: 0 on success or negative errno.
  1422. */
  1423. int nvmem_cell_read_variable_le_u64(struct device *dev, const char *cell_id,
  1424. u64 *val)
  1425. {
  1426. size_t len;
  1427. const u8 *buf;
  1428. int i;
  1429. buf = nvmem_cell_read_variable_common(dev, cell_id, sizeof(*val), &len);
  1430. if (IS_ERR(buf))
  1431. return PTR_ERR(buf);
  1432. /* Copy w/ implicit endian conversion */
  1433. *val = 0;
  1434. for (i = 0; i < len; i++)
  1435. *val |= (uint64_t)buf[i] << (8 * i);
  1436. kfree(buf);
  1437. return 0;
  1438. }
  1439. EXPORT_SYMBOL_GPL(nvmem_cell_read_variable_le_u64);
  1440. /**
  1441. * nvmem_device_cell_read() - Read a given nvmem device and cell
  1442. *
  1443. * @nvmem: nvmem device to read from.
  1444. * @info: nvmem cell info to be read.
  1445. * @buf: buffer pointer which will be populated on successful read.
  1446. *
  1447. * Return: length of successful bytes read on success and negative
  1448. * error code on error.
  1449. */
  1450. ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
  1451. struct nvmem_cell_info *info, void *buf)
  1452. {
  1453. struct nvmem_cell_entry cell;
  1454. int rc;
  1455. ssize_t len;
  1456. if (!nvmem)
  1457. return -EINVAL;
  1458. rc = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, &cell);
  1459. if (rc)
  1460. return rc;
  1461. rc = __nvmem_cell_read(nvmem, &cell, buf, &len, NULL);
  1462. if (rc)
  1463. return rc;
  1464. return len;
  1465. }
  1466. EXPORT_SYMBOL_GPL(nvmem_device_cell_read);
  1467. /**
  1468. * nvmem_device_cell_write() - Write cell to a given nvmem device
  1469. *
  1470. * @nvmem: nvmem device to be written to.
  1471. * @info: nvmem cell info to be written.
  1472. * @buf: buffer to be written to cell.
  1473. *
  1474. * Return: length of bytes written or negative error code on failure.
  1475. */
  1476. int nvmem_device_cell_write(struct nvmem_device *nvmem,
  1477. struct nvmem_cell_info *info, void *buf)
  1478. {
  1479. struct nvmem_cell_entry cell;
  1480. int rc;
  1481. if (!nvmem)
  1482. return -EINVAL;
  1483. rc = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, &cell);
  1484. if (rc)
  1485. return rc;
  1486. return __nvmem_cell_entry_write(&cell, buf, cell.bytes);
  1487. }
  1488. EXPORT_SYMBOL_GPL(nvmem_device_cell_write);
  1489. /**
  1490. * nvmem_device_read() - Read from a given nvmem device
  1491. *
  1492. * @nvmem: nvmem device to read from.
  1493. * @offset: offset in nvmem device.
  1494. * @bytes: number of bytes to read.
  1495. * @buf: buffer pointer which will be populated on successful read.
  1496. *
  1497. * Return: length of successful bytes read on success and negative
  1498. * error code on error.
  1499. */
  1500. int nvmem_device_read(struct nvmem_device *nvmem,
  1501. unsigned int offset,
  1502. size_t bytes, void *buf)
  1503. {
  1504. int rc;
  1505. if (!nvmem)
  1506. return -EINVAL;
  1507. rc = nvmem_reg_read(nvmem, offset, buf, bytes);
  1508. if (rc)
  1509. return rc;
  1510. return bytes;
  1511. }
  1512. EXPORT_SYMBOL_GPL(nvmem_device_read);
  1513. /**
  1514. * nvmem_device_write() - Write cell to a given nvmem device
  1515. *
  1516. * @nvmem: nvmem device to be written to.
  1517. * @offset: offset in nvmem device.
  1518. * @bytes: number of bytes to write.
  1519. * @buf: buffer to be written.
  1520. *
  1521. * Return: length of bytes written or negative error code on failure.
  1522. */
  1523. int nvmem_device_write(struct nvmem_device *nvmem,
  1524. unsigned int offset,
  1525. size_t bytes, void *buf)
  1526. {
  1527. int rc;
  1528. if (!nvmem)
  1529. return -EINVAL;
  1530. rc = nvmem_reg_write(nvmem, offset, buf, bytes);
  1531. if (rc)
  1532. return rc;
  1533. return bytes;
  1534. }
  1535. EXPORT_SYMBOL_GPL(nvmem_device_write);
  1536. /**
  1537. * nvmem_add_cell_table() - register a table of cell info entries
  1538. *
  1539. * @table: table of cell info entries
  1540. */
  1541. void nvmem_add_cell_table(struct nvmem_cell_table *table)
  1542. {
  1543. mutex_lock(&nvmem_cell_mutex);
  1544. list_add_tail(&table->node, &nvmem_cell_tables);
  1545. mutex_unlock(&nvmem_cell_mutex);
  1546. }
  1547. EXPORT_SYMBOL_GPL(nvmem_add_cell_table);
  1548. /**
  1549. * nvmem_del_cell_table() - remove a previously registered cell info table
  1550. *
  1551. * @table: table of cell info entries
  1552. */
  1553. void nvmem_del_cell_table(struct nvmem_cell_table *table)
  1554. {
  1555. mutex_lock(&nvmem_cell_mutex);
  1556. list_del(&table->node);
  1557. mutex_unlock(&nvmem_cell_mutex);
  1558. }
  1559. EXPORT_SYMBOL_GPL(nvmem_del_cell_table);
  1560. /**
  1561. * nvmem_add_cell_lookups() - register a list of cell lookup entries
  1562. *
  1563. * @entries: array of cell lookup entries
  1564. * @nentries: number of cell lookup entries in the array
  1565. */
  1566. void nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
  1567. {
  1568. int i;
  1569. mutex_lock(&nvmem_lookup_mutex);
  1570. for (i = 0; i < nentries; i++)
  1571. list_add_tail(&entries[i].node, &nvmem_lookup_list);
  1572. mutex_unlock(&nvmem_lookup_mutex);
  1573. }
  1574. EXPORT_SYMBOL_GPL(nvmem_add_cell_lookups);
  1575. /**
  1576. * nvmem_del_cell_lookups() - remove a list of previously added cell lookup
  1577. * entries
  1578. *
  1579. * @entries: array of cell lookup entries
  1580. * @nentries: number of cell lookup entries in the array
  1581. */
  1582. void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
  1583. {
  1584. int i;
  1585. mutex_lock(&nvmem_lookup_mutex);
  1586. for (i = 0; i < nentries; i++)
  1587. list_del(&entries[i].node);
  1588. mutex_unlock(&nvmem_lookup_mutex);
  1589. }
  1590. EXPORT_SYMBOL_GPL(nvmem_del_cell_lookups);
  1591. /**
  1592. * nvmem_dev_name() - Get the name of a given nvmem device.
  1593. *
  1594. * @nvmem: nvmem device.
  1595. *
  1596. * Return: name of the nvmem device.
  1597. */
  1598. const char *nvmem_dev_name(struct nvmem_device *nvmem)
  1599. {
  1600. return dev_name(&nvmem->dev);
  1601. }
  1602. EXPORT_SYMBOL_GPL(nvmem_dev_name);
  1603. static int __init nvmem_init(void)
  1604. {
  1605. return bus_register(&nvmem_bus_type);
  1606. }
  1607. static void __exit nvmem_exit(void)
  1608. {
  1609. bus_unregister(&nvmem_bus_type);
  1610. }
  1611. subsys_initcall(nvmem_init);
  1612. module_exit(nvmem_exit);
  1613. MODULE_AUTHOR("Srinivas Kandagatla <[email protected]");
  1614. MODULE_AUTHOR("Maxime Ripard <[email protected]");
  1615. MODULE_DESCRIPTION("nvmem Driver Core");
  1616. MODULE_LICENSE("GPL v2");