core.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283
  1. /*
  2. * nvmem framework core.
  3. *
  4. * Copyright (C) 2015 Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
  5. * Copyright (C) 2013 Maxime Ripard <maxime.ripard@free-electrons.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 and
  9. * only version 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/device.h>
  17. #include <linux/export.h>
  18. #include <linux/fs.h>
  19. #include <linux/idr.h>
  20. #include <linux/init.h>
  21. #include <linux/module.h>
  22. #include <linux/nvmem-consumer.h>
  23. #include <linux/nvmem-provider.h>
  24. #include <linux/of.h>
  25. #include <linux/slab.h>
  26. struct nvmem_device {
  27. const char *name;
  28. struct module *owner;
  29. struct device dev;
  30. int stride;
  31. int word_size;
  32. int ncells;
  33. int id;
  34. int users;
  35. size_t size;
  36. bool read_only;
  37. int flags;
  38. struct bin_attribute eeprom;
  39. struct device *base_dev;
  40. nvmem_reg_read_t reg_read;
  41. nvmem_reg_write_t reg_write;
  42. void *priv;
  43. };
  44. #define FLAG_COMPAT BIT(0)
  45. struct nvmem_cell {
  46. const char *name;
  47. int offset;
  48. int bytes;
  49. int bit_offset;
  50. int nbits;
  51. struct nvmem_device *nvmem;
  52. struct list_head node;
  53. };
  54. static DEFINE_MUTEX(nvmem_mutex);
  55. static DEFINE_IDA(nvmem_ida);
  56. static LIST_HEAD(nvmem_cells);
  57. static DEFINE_MUTEX(nvmem_cells_mutex);
  58. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  59. static struct lock_class_key eeprom_lock_key;
  60. #endif
  61. #define to_nvmem_device(d) container_of(d, struct nvmem_device, dev)
  62. static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
  63. void *val, size_t bytes)
  64. {
  65. if (nvmem->reg_read)
  66. return nvmem->reg_read(nvmem->priv, offset, val, bytes);
  67. return -EINVAL;
  68. }
  69. static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
  70. void *val, size_t bytes)
  71. {
  72. if (nvmem->reg_write)
  73. return nvmem->reg_write(nvmem->priv, offset, val, bytes);
  74. return -EINVAL;
  75. }
  76. static ssize_t bin_attr_nvmem_read(struct file *filp, struct kobject *kobj,
  77. struct bin_attribute *attr,
  78. char *buf, loff_t pos, size_t count)
  79. {
  80. struct device *dev;
  81. struct nvmem_device *nvmem;
  82. int rc;
  83. if (attr->private)
  84. dev = attr->private;
  85. else
  86. dev = container_of(kobj, struct device, kobj);
  87. nvmem = to_nvmem_device(dev);
  88. /* Stop the user from reading */
  89. if (pos >= nvmem->size)
  90. return 0;
  91. if (count < nvmem->word_size)
  92. return -EINVAL;
  93. if (pos + count > nvmem->size)
  94. count = nvmem->size - pos;
  95. count = round_down(count, nvmem->word_size);
  96. rc = nvmem_reg_read(nvmem, pos, buf, count);
  97. if (rc)
  98. return rc;
  99. return count;
  100. }
  101. static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj,
  102. struct bin_attribute *attr,
  103. char *buf, loff_t pos, size_t count)
  104. {
  105. struct device *dev;
  106. struct nvmem_device *nvmem;
  107. int rc;
  108. if (attr->private)
  109. dev = attr->private;
  110. else
  111. dev = container_of(kobj, struct device, kobj);
  112. nvmem = to_nvmem_device(dev);
  113. /* Stop the user from writing */
  114. if (pos >= nvmem->size)
  115. return -EFBIG;
  116. if (count < nvmem->word_size)
  117. return -EINVAL;
  118. if (pos + count > nvmem->size)
  119. count = nvmem->size - pos;
  120. count = round_down(count, nvmem->word_size);
  121. rc = nvmem_reg_write(nvmem, pos, buf, count);
  122. if (rc)
  123. return rc;
  124. return count;
  125. }
  126. /* default read/write permissions */
  127. static struct bin_attribute bin_attr_rw_nvmem = {
  128. .attr = {
  129. .name = "nvmem",
  130. .mode = S_IWUSR | S_IRUGO,
  131. },
  132. .read = bin_attr_nvmem_read,
  133. .write = bin_attr_nvmem_write,
  134. };
  135. static struct bin_attribute *nvmem_bin_rw_attributes[] = {
  136. &bin_attr_rw_nvmem,
  137. NULL,
  138. };
  139. static const struct attribute_group nvmem_bin_rw_group = {
  140. .bin_attrs = nvmem_bin_rw_attributes,
  141. };
  142. static const struct attribute_group *nvmem_rw_dev_groups[] = {
  143. &nvmem_bin_rw_group,
  144. NULL,
  145. };
  146. /* read only permission */
  147. static struct bin_attribute bin_attr_ro_nvmem = {
  148. .attr = {
  149. .name = "nvmem",
  150. .mode = S_IRUGO,
  151. },
  152. .read = bin_attr_nvmem_read,
  153. };
  154. static struct bin_attribute *nvmem_bin_ro_attributes[] = {
  155. &bin_attr_ro_nvmem,
  156. NULL,
  157. };
  158. static const struct attribute_group nvmem_bin_ro_group = {
  159. .bin_attrs = nvmem_bin_ro_attributes,
  160. };
  161. static const struct attribute_group *nvmem_ro_dev_groups[] = {
  162. &nvmem_bin_ro_group,
  163. NULL,
  164. };
  165. /* default read/write permissions, root only */
  166. static struct bin_attribute bin_attr_rw_root_nvmem = {
  167. .attr = {
  168. .name = "nvmem",
  169. .mode = S_IWUSR | S_IRUSR,
  170. },
  171. .read = bin_attr_nvmem_read,
  172. .write = bin_attr_nvmem_write,
  173. };
  174. static struct bin_attribute *nvmem_bin_rw_root_attributes[] = {
  175. &bin_attr_rw_root_nvmem,
  176. NULL,
  177. };
  178. static const struct attribute_group nvmem_bin_rw_root_group = {
  179. .bin_attrs = nvmem_bin_rw_root_attributes,
  180. };
  181. static const struct attribute_group *nvmem_rw_root_dev_groups[] = {
  182. &nvmem_bin_rw_root_group,
  183. NULL,
  184. };
  185. /* read only permission, root only */
  186. static struct bin_attribute bin_attr_ro_root_nvmem = {
  187. .attr = {
  188. .name = "nvmem",
  189. .mode = S_IRUSR,
  190. },
  191. .read = bin_attr_nvmem_read,
  192. };
  193. static struct bin_attribute *nvmem_bin_ro_root_attributes[] = {
  194. &bin_attr_ro_root_nvmem,
  195. NULL,
  196. };
  197. static const struct attribute_group nvmem_bin_ro_root_group = {
  198. .bin_attrs = nvmem_bin_ro_root_attributes,
  199. };
  200. static const struct attribute_group *nvmem_ro_root_dev_groups[] = {
  201. &nvmem_bin_ro_root_group,
  202. NULL,
  203. };
  204. static void nvmem_release(struct device *dev)
  205. {
  206. struct nvmem_device *nvmem = to_nvmem_device(dev);
  207. ida_simple_remove(&nvmem_ida, nvmem->id);
  208. kfree(nvmem);
  209. }
  210. static const struct device_type nvmem_provider_type = {
  211. .release = nvmem_release,
  212. };
  213. static struct bus_type nvmem_bus_type = {
  214. .name = "nvmem",
  215. };
  216. static int of_nvmem_match(struct device *dev, void *nvmem_np)
  217. {
  218. return dev->of_node == nvmem_np;
  219. }
  220. static struct nvmem_device *of_nvmem_find(struct device_node *nvmem_np)
  221. {
  222. struct device *d;
  223. if (!nvmem_np)
  224. return NULL;
  225. d = bus_find_device(&nvmem_bus_type, NULL, nvmem_np, of_nvmem_match);
  226. if (!d)
  227. return NULL;
  228. return to_nvmem_device(d);
  229. }
  230. static struct nvmem_cell *nvmem_find_cell(const char *cell_id)
  231. {
  232. struct nvmem_cell *p;
  233. mutex_lock(&nvmem_cells_mutex);
  234. list_for_each_entry(p, &nvmem_cells, node)
  235. if (!strcmp(p->name, cell_id)) {
  236. mutex_unlock(&nvmem_cells_mutex);
  237. return p;
  238. }
  239. mutex_unlock(&nvmem_cells_mutex);
  240. return NULL;
  241. }
  242. static void nvmem_cell_drop(struct nvmem_cell *cell)
  243. {
  244. mutex_lock(&nvmem_cells_mutex);
  245. list_del(&cell->node);
  246. mutex_unlock(&nvmem_cells_mutex);
  247. kfree(cell);
  248. }
  249. static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem)
  250. {
  251. struct nvmem_cell *cell;
  252. struct list_head *p, *n;
  253. list_for_each_safe(p, n, &nvmem_cells) {
  254. cell = list_entry(p, struct nvmem_cell, node);
  255. if (cell->nvmem == nvmem)
  256. nvmem_cell_drop(cell);
  257. }
  258. }
  259. static void nvmem_cell_add(struct nvmem_cell *cell)
  260. {
  261. mutex_lock(&nvmem_cells_mutex);
  262. list_add_tail(&cell->node, &nvmem_cells);
  263. mutex_unlock(&nvmem_cells_mutex);
  264. }
  265. static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem,
  266. const struct nvmem_cell_info *info,
  267. struct nvmem_cell *cell)
  268. {
  269. cell->nvmem = nvmem;
  270. cell->offset = info->offset;
  271. cell->bytes = info->bytes;
  272. cell->name = info->name;
  273. cell->bit_offset = info->bit_offset;
  274. cell->nbits = info->nbits;
  275. if (cell->nbits)
  276. cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset,
  277. BITS_PER_BYTE);
  278. if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
  279. dev_err(&nvmem->dev,
  280. "cell %s unaligned to nvmem stride %d\n",
  281. cell->name, nvmem->stride);
  282. return -EINVAL;
  283. }
  284. return 0;
  285. }
  286. static int nvmem_add_cells(struct nvmem_device *nvmem,
  287. const struct nvmem_config *cfg)
  288. {
  289. struct nvmem_cell **cells;
  290. const struct nvmem_cell_info *info = cfg->cells;
  291. int i, rval;
  292. cells = kcalloc(cfg->ncells, sizeof(*cells), GFP_KERNEL);
  293. if (!cells)
  294. return -ENOMEM;
  295. for (i = 0; i < cfg->ncells; i++) {
  296. cells[i] = kzalloc(sizeof(**cells), GFP_KERNEL);
  297. if (!cells[i]) {
  298. rval = -ENOMEM;
  299. goto err;
  300. }
  301. rval = nvmem_cell_info_to_nvmem_cell(nvmem, &info[i], cells[i]);
  302. if (rval) {
  303. kfree(cells[i]);
  304. goto err;
  305. }
  306. nvmem_cell_add(cells[i]);
  307. }
  308. nvmem->ncells = cfg->ncells;
  309. /* remove tmp array */
  310. kfree(cells);
  311. return 0;
  312. err:
  313. while (i--)
  314. nvmem_cell_drop(cells[i]);
  315. kfree(cells);
  316. return rval;
  317. }
  318. /*
  319. * nvmem_setup_compat() - Create an additional binary entry in
  320. * drivers sys directory, to be backwards compatible with the older
  321. * drivers/misc/eeprom drivers.
  322. */
  323. static int nvmem_setup_compat(struct nvmem_device *nvmem,
  324. const struct nvmem_config *config)
  325. {
  326. int rval;
  327. if (!config->base_dev)
  328. return -EINVAL;
  329. if (nvmem->read_only)
  330. nvmem->eeprom = bin_attr_ro_root_nvmem;
  331. else
  332. nvmem->eeprom = bin_attr_rw_root_nvmem;
  333. nvmem->eeprom.attr.name = "eeprom";
  334. nvmem->eeprom.size = nvmem->size;
  335. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  336. nvmem->eeprom.attr.key = &eeprom_lock_key;
  337. #endif
  338. nvmem->eeprom.private = &nvmem->dev;
  339. nvmem->base_dev = config->base_dev;
  340. rval = device_create_bin_file(nvmem->base_dev, &nvmem->eeprom);
  341. if (rval) {
  342. dev_err(&nvmem->dev,
  343. "Failed to create eeprom binary file %d\n", rval);
  344. return rval;
  345. }
  346. nvmem->flags |= FLAG_COMPAT;
  347. return 0;
  348. }
  349. /**
  350. * nvmem_register() - Register a nvmem device for given nvmem_config.
  351. * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
  352. *
  353. * @config: nvmem device configuration with which nvmem device is created.
  354. *
  355. * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
  356. * on success.
  357. */
  358. struct nvmem_device *nvmem_register(const struct nvmem_config *config)
  359. {
  360. struct nvmem_device *nvmem;
  361. struct device_node *np;
  362. int rval;
  363. if (!config->dev)
  364. return ERR_PTR(-EINVAL);
  365. nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL);
  366. if (!nvmem)
  367. return ERR_PTR(-ENOMEM);
  368. rval = ida_simple_get(&nvmem_ida, 0, 0, GFP_KERNEL);
  369. if (rval < 0) {
  370. kfree(nvmem);
  371. return ERR_PTR(rval);
  372. }
  373. nvmem->id = rval;
  374. nvmem->owner = config->owner;
  375. nvmem->stride = config->stride;
  376. nvmem->word_size = config->word_size;
  377. nvmem->size = config->size;
  378. nvmem->dev.type = &nvmem_provider_type;
  379. nvmem->dev.bus = &nvmem_bus_type;
  380. nvmem->dev.parent = config->dev;
  381. nvmem->priv = config->priv;
  382. nvmem->reg_read = config->reg_read;
  383. nvmem->reg_write = config->reg_write;
  384. np = config->dev->of_node;
  385. nvmem->dev.of_node = np;
  386. dev_set_name(&nvmem->dev, "%s%d",
  387. config->name ? : "nvmem",
  388. config->name ? config->id : nvmem->id);
  389. nvmem->read_only = of_property_read_bool(np, "read-only") |
  390. config->read_only;
  391. if (config->root_only)
  392. nvmem->dev.groups = nvmem->read_only ?
  393. nvmem_ro_root_dev_groups :
  394. nvmem_rw_root_dev_groups;
  395. else
  396. nvmem->dev.groups = nvmem->read_only ?
  397. nvmem_ro_dev_groups :
  398. nvmem_rw_dev_groups;
  399. device_initialize(&nvmem->dev);
  400. dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
  401. rval = device_add(&nvmem->dev);
  402. if (rval)
  403. goto err_put_device;
  404. if (config->compat) {
  405. rval = nvmem_setup_compat(nvmem, config);
  406. if (rval)
  407. goto err_device_del;
  408. }
  409. if (config->cells)
  410. nvmem_add_cells(nvmem, config);
  411. return nvmem;
  412. err_device_del:
  413. device_del(&nvmem->dev);
  414. err_put_device:
  415. put_device(&nvmem->dev);
  416. return ERR_PTR(rval);
  417. }
  418. EXPORT_SYMBOL_GPL(nvmem_register);
  419. /**
  420. * nvmem_unregister() - Unregister previously registered nvmem device
  421. *
  422. * @nvmem: Pointer to previously registered nvmem device.
  423. *
  424. * Return: Will be an negative on error or a zero on success.
  425. */
  426. int nvmem_unregister(struct nvmem_device *nvmem)
  427. {
  428. mutex_lock(&nvmem_mutex);
  429. if (nvmem->users) {
  430. mutex_unlock(&nvmem_mutex);
  431. return -EBUSY;
  432. }
  433. mutex_unlock(&nvmem_mutex);
  434. if (nvmem->flags & FLAG_COMPAT)
  435. device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
  436. nvmem_device_remove_all_cells(nvmem);
  437. device_del(&nvmem->dev);
  438. put_device(&nvmem->dev);
  439. return 0;
  440. }
  441. EXPORT_SYMBOL_GPL(nvmem_unregister);
  442. static struct nvmem_device *__nvmem_device_get(struct device_node *np,
  443. struct nvmem_cell **cellp,
  444. const char *cell_id)
  445. {
  446. struct nvmem_device *nvmem = NULL;
  447. mutex_lock(&nvmem_mutex);
  448. if (np) {
  449. nvmem = of_nvmem_find(np);
  450. if (!nvmem) {
  451. mutex_unlock(&nvmem_mutex);
  452. return ERR_PTR(-EPROBE_DEFER);
  453. }
  454. } else {
  455. struct nvmem_cell *cell = nvmem_find_cell(cell_id);
  456. if (cell) {
  457. nvmem = cell->nvmem;
  458. *cellp = cell;
  459. }
  460. if (!nvmem) {
  461. mutex_unlock(&nvmem_mutex);
  462. return ERR_PTR(-ENOENT);
  463. }
  464. }
  465. nvmem->users++;
  466. mutex_unlock(&nvmem_mutex);
  467. if (!try_module_get(nvmem->owner)) {
  468. dev_err(&nvmem->dev,
  469. "could not increase module refcount for cell %s\n",
  470. nvmem->name);
  471. mutex_lock(&nvmem_mutex);
  472. nvmem->users--;
  473. mutex_unlock(&nvmem_mutex);
  474. return ERR_PTR(-EINVAL);
  475. }
  476. return nvmem;
  477. }
  478. static void __nvmem_device_put(struct nvmem_device *nvmem)
  479. {
  480. module_put(nvmem->owner);
  481. mutex_lock(&nvmem_mutex);
  482. nvmem->users--;
  483. mutex_unlock(&nvmem_mutex);
  484. }
  485. static int nvmem_match(struct device *dev, void *data)
  486. {
  487. return !strcmp(dev_name(dev), data);
  488. }
  489. static struct nvmem_device *nvmem_find(const char *name)
  490. {
  491. struct device *d;
  492. d = bus_find_device(&nvmem_bus_type, NULL, (void *)name, nvmem_match);
  493. if (!d)
  494. return NULL;
  495. return to_nvmem_device(d);
  496. }
  497. #if IS_ENABLED(CONFIG_NVMEM) && IS_ENABLED(CONFIG_OF)
  498. /**
  499. * of_nvmem_device_get() - Get nvmem device from a given id
  500. *
  501. * @np: Device tree node that uses the nvmem device.
  502. * @id: nvmem name from nvmem-names property.
  503. *
  504. * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
  505. * on success.
  506. */
  507. struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id)
  508. {
  509. struct device_node *nvmem_np;
  510. int index;
  511. index = of_property_match_string(np, "nvmem-names", id);
  512. nvmem_np = of_parse_phandle(np, "nvmem", index);
  513. if (!nvmem_np)
  514. return ERR_PTR(-EINVAL);
  515. return __nvmem_device_get(nvmem_np, NULL, NULL);
  516. }
  517. EXPORT_SYMBOL_GPL(of_nvmem_device_get);
  518. #endif
  519. /**
  520. * nvmem_device_get() - Get nvmem device from a given id
  521. *
  522. * @dev: Device that uses the nvmem device.
  523. * @dev_name: name of the requested nvmem device.
  524. *
  525. * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
  526. * on success.
  527. */
  528. struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name)
  529. {
  530. if (dev->of_node) { /* try dt first */
  531. struct nvmem_device *nvmem;
  532. nvmem = of_nvmem_device_get(dev->of_node, dev_name);
  533. if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER)
  534. return nvmem;
  535. }
  536. return nvmem_find(dev_name);
  537. }
  538. EXPORT_SYMBOL_GPL(nvmem_device_get);
  539. static int devm_nvmem_device_match(struct device *dev, void *res, void *data)
  540. {
  541. struct nvmem_device **nvmem = res;
  542. if (WARN_ON(!nvmem || !*nvmem))
  543. return 0;
  544. return *nvmem == data;
  545. }
  546. static void devm_nvmem_device_release(struct device *dev, void *res)
  547. {
  548. nvmem_device_put(*(struct nvmem_device **)res);
  549. }
  550. /**
  551. * devm_nvmem_device_put() - put alredy got nvmem device
  552. *
  553. * @dev: Device that uses the nvmem device.
  554. * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
  555. * that needs to be released.
  556. */
  557. void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)
  558. {
  559. int ret;
  560. ret = devres_release(dev, devm_nvmem_device_release,
  561. devm_nvmem_device_match, nvmem);
  562. WARN_ON(ret);
  563. }
  564. EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
  565. /**
  566. * nvmem_device_put() - put alredy got nvmem device
  567. *
  568. * @nvmem: pointer to nvmem device that needs to be released.
  569. */
  570. void nvmem_device_put(struct nvmem_device *nvmem)
  571. {
  572. __nvmem_device_put(nvmem);
  573. }
  574. EXPORT_SYMBOL_GPL(nvmem_device_put);
  575. /**
  576. * devm_nvmem_device_get() - Get nvmem cell of device form a given id
  577. *
  578. * @dev: Device that requests the nvmem device.
  579. * @id: name id for the requested nvmem device.
  580. *
  581. * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell
  582. * on success. The nvmem_cell will be freed by the automatically once the
  583. * device is freed.
  584. */
  585. struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id)
  586. {
  587. struct nvmem_device **ptr, *nvmem;
  588. ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL);
  589. if (!ptr)
  590. return ERR_PTR(-ENOMEM);
  591. nvmem = nvmem_device_get(dev, id);
  592. if (!IS_ERR(nvmem)) {
  593. *ptr = nvmem;
  594. devres_add(dev, ptr);
  595. } else {
  596. devres_free(ptr);
  597. }
  598. return nvmem;
  599. }
  600. EXPORT_SYMBOL_GPL(devm_nvmem_device_get);
  601. static struct nvmem_cell *nvmem_cell_get_from_list(const char *cell_id)
  602. {
  603. struct nvmem_cell *cell = NULL;
  604. struct nvmem_device *nvmem;
  605. nvmem = __nvmem_device_get(NULL, &cell, cell_id);
  606. if (IS_ERR(nvmem))
  607. return ERR_CAST(nvmem);
  608. return cell;
  609. }
  610. #if IS_ENABLED(CONFIG_NVMEM) && IS_ENABLED(CONFIG_OF)
  611. /**
  612. * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
  613. *
  614. * @np: Device tree node that uses the nvmem cell.
  615. * @name: nvmem cell name from nvmem-cell-names property, or NULL
  616. * for the cell at index 0 (the lone cell with no accompanying
  617. * nvmem-cell-names property).
  618. *
  619. * Return: Will be an ERR_PTR() on error or a valid pointer
  620. * to a struct nvmem_cell. The nvmem_cell will be freed by the
  621. * nvmem_cell_put().
  622. */
  623. struct nvmem_cell *of_nvmem_cell_get(struct device_node *np,
  624. const char *name)
  625. {
  626. struct device_node *cell_np, *nvmem_np;
  627. struct nvmem_cell *cell;
  628. struct nvmem_device *nvmem;
  629. const __be32 *addr;
  630. int rval, len;
  631. int index = 0;
  632. /* if cell name exists, find index to the name */
  633. if (name)
  634. index = of_property_match_string(np, "nvmem-cell-names", name);
  635. cell_np = of_parse_phandle(np, "nvmem-cells", index);
  636. if (!cell_np)
  637. return ERR_PTR(-EINVAL);
  638. nvmem_np = of_get_next_parent(cell_np);
  639. if (!nvmem_np)
  640. return ERR_PTR(-EINVAL);
  641. nvmem = __nvmem_device_get(nvmem_np, NULL, NULL);
  642. of_node_put(nvmem_np);
  643. if (IS_ERR(nvmem))
  644. return ERR_CAST(nvmem);
  645. addr = of_get_property(cell_np, "reg", &len);
  646. if (!addr || (len < 2 * sizeof(u32))) {
  647. dev_err(&nvmem->dev, "nvmem: invalid reg on %pOF\n",
  648. cell_np);
  649. rval = -EINVAL;
  650. goto err_mem;
  651. }
  652. cell = kzalloc(sizeof(*cell), GFP_KERNEL);
  653. if (!cell) {
  654. rval = -ENOMEM;
  655. goto err_mem;
  656. }
  657. cell->nvmem = nvmem;
  658. cell->offset = be32_to_cpup(addr++);
  659. cell->bytes = be32_to_cpup(addr);
  660. cell->name = cell_np->name;
  661. addr = of_get_property(cell_np, "bits", &len);
  662. if (addr && len == (2 * sizeof(u32))) {
  663. cell->bit_offset = be32_to_cpup(addr++);
  664. cell->nbits = be32_to_cpup(addr);
  665. }
  666. if (cell->nbits)
  667. cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset,
  668. BITS_PER_BYTE);
  669. if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
  670. dev_err(&nvmem->dev,
  671. "cell %s unaligned to nvmem stride %d\n",
  672. cell->name, nvmem->stride);
  673. rval = -EINVAL;
  674. goto err_sanity;
  675. }
  676. nvmem_cell_add(cell);
  677. return cell;
  678. err_sanity:
  679. kfree(cell);
  680. err_mem:
  681. __nvmem_device_put(nvmem);
  682. return ERR_PTR(rval);
  683. }
  684. EXPORT_SYMBOL_GPL(of_nvmem_cell_get);
  685. #endif
  686. /**
  687. * nvmem_cell_get() - Get nvmem cell of device form a given cell name
  688. *
  689. * @dev: Device that requests the nvmem cell.
  690. * @cell_id: nvmem cell name to get.
  691. *
  692. * Return: Will be an ERR_PTR() on error or a valid pointer
  693. * to a struct nvmem_cell. The nvmem_cell will be freed by the
  694. * nvmem_cell_put().
  695. */
  696. struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *cell_id)
  697. {
  698. struct nvmem_cell *cell;
  699. if (dev->of_node) { /* try dt first */
  700. cell = of_nvmem_cell_get(dev->of_node, cell_id);
  701. if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER)
  702. return cell;
  703. }
  704. return nvmem_cell_get_from_list(cell_id);
  705. }
  706. EXPORT_SYMBOL_GPL(nvmem_cell_get);
  707. static void devm_nvmem_cell_release(struct device *dev, void *res)
  708. {
  709. nvmem_cell_put(*(struct nvmem_cell **)res);
  710. }
  711. /**
  712. * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
  713. *
  714. * @dev: Device that requests the nvmem cell.
  715. * @id: nvmem cell name id to get.
  716. *
  717. * Return: Will be an ERR_PTR() on error or a valid pointer
  718. * to a struct nvmem_cell. The nvmem_cell will be freed by the
  719. * automatically once the device is freed.
  720. */
  721. struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id)
  722. {
  723. struct nvmem_cell **ptr, *cell;
  724. ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL);
  725. if (!ptr)
  726. return ERR_PTR(-ENOMEM);
  727. cell = nvmem_cell_get(dev, id);
  728. if (!IS_ERR(cell)) {
  729. *ptr = cell;
  730. devres_add(dev, ptr);
  731. } else {
  732. devres_free(ptr);
  733. }
  734. return cell;
  735. }
  736. EXPORT_SYMBOL_GPL(devm_nvmem_cell_get);
  737. static int devm_nvmem_cell_match(struct device *dev, void *res, void *data)
  738. {
  739. struct nvmem_cell **c = res;
  740. if (WARN_ON(!c || !*c))
  741. return 0;
  742. return *c == data;
  743. }
  744. /**
  745. * devm_nvmem_cell_put() - Release previously allocated nvmem cell
  746. * from devm_nvmem_cell_get.
  747. *
  748. * @dev: Device that requests the nvmem cell.
  749. * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
  750. */
  751. void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell)
  752. {
  753. int ret;
  754. ret = devres_release(dev, devm_nvmem_cell_release,
  755. devm_nvmem_cell_match, cell);
  756. WARN_ON(ret);
  757. }
  758. EXPORT_SYMBOL(devm_nvmem_cell_put);
  759. /**
  760. * nvmem_cell_put() - Release previously allocated nvmem cell.
  761. *
  762. * @cell: Previously allocated nvmem cell by nvmem_cell_get().
  763. */
  764. void nvmem_cell_put(struct nvmem_cell *cell)
  765. {
  766. struct nvmem_device *nvmem = cell->nvmem;
  767. __nvmem_device_put(nvmem);
  768. nvmem_cell_drop(cell);
  769. }
  770. EXPORT_SYMBOL_GPL(nvmem_cell_put);
  771. static inline void nvmem_shift_read_buffer_in_place(struct nvmem_cell *cell,
  772. void *buf)
  773. {
  774. u8 *p, *b;
  775. int i, bit_offset = cell->bit_offset;
  776. p = b = buf;
  777. if (bit_offset) {
  778. /* First shift */
  779. *b++ >>= bit_offset;
  780. /* setup rest of the bytes if any */
  781. for (i = 1; i < cell->bytes; i++) {
  782. /* Get bits from next byte and shift them towards msb */
  783. *p |= *b << (BITS_PER_BYTE - bit_offset);
  784. p = b;
  785. *b++ >>= bit_offset;
  786. }
  787. /* result fits in less bytes */
  788. if (cell->bytes != DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE))
  789. *p-- = 0;
  790. }
  791. /* clear msb bits if any leftover in the last byte */
  792. *p &= GENMASK((cell->nbits%BITS_PER_BYTE) - 1, 0);
  793. }
  794. static int __nvmem_cell_read(struct nvmem_device *nvmem,
  795. struct nvmem_cell *cell,
  796. void *buf, size_t *len)
  797. {
  798. int rc;
  799. rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->bytes);
  800. if (rc)
  801. return rc;
  802. /* shift bits in-place */
  803. if (cell->bit_offset || cell->nbits)
  804. nvmem_shift_read_buffer_in_place(cell, buf);
  805. if (len)
  806. *len = cell->bytes;
  807. return 0;
  808. }
  809. /**
  810. * nvmem_cell_read() - Read a given nvmem cell
  811. *
  812. * @cell: nvmem cell to be read.
  813. * @len: pointer to length of cell which will be populated on successful read;
  814. * can be NULL.
  815. *
  816. * Return: ERR_PTR() on error or a valid pointer to a buffer on success. The
  817. * buffer should be freed by the consumer with a kfree().
  818. */
  819. void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
  820. {
  821. struct nvmem_device *nvmem = cell->nvmem;
  822. u8 *buf;
  823. int rc;
  824. if (!nvmem)
  825. return ERR_PTR(-EINVAL);
  826. buf = kzalloc(cell->bytes, GFP_KERNEL);
  827. if (!buf)
  828. return ERR_PTR(-ENOMEM);
  829. rc = __nvmem_cell_read(nvmem, cell, buf, len);
  830. if (rc) {
  831. kfree(buf);
  832. return ERR_PTR(rc);
  833. }
  834. return buf;
  835. }
  836. EXPORT_SYMBOL_GPL(nvmem_cell_read);
  837. static inline void *nvmem_cell_prepare_write_buffer(struct nvmem_cell *cell,
  838. u8 *_buf, int len)
  839. {
  840. struct nvmem_device *nvmem = cell->nvmem;
  841. int i, rc, nbits, bit_offset = cell->bit_offset;
  842. u8 v, *p, *buf, *b, pbyte, pbits;
  843. nbits = cell->nbits;
  844. buf = kzalloc(cell->bytes, GFP_KERNEL);
  845. if (!buf)
  846. return ERR_PTR(-ENOMEM);
  847. memcpy(buf, _buf, len);
  848. p = b = buf;
  849. if (bit_offset) {
  850. pbyte = *b;
  851. *b <<= bit_offset;
  852. /* setup the first byte with lsb bits from nvmem */
  853. rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
  854. *b++ |= GENMASK(bit_offset - 1, 0) & v;
  855. /* setup rest of the byte if any */
  856. for (i = 1; i < cell->bytes; i++) {
  857. /* Get last byte bits and shift them towards lsb */
  858. pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset);
  859. pbyte = *b;
  860. p = b;
  861. *b <<= bit_offset;
  862. *b++ |= pbits;
  863. }
  864. }
  865. /* if it's not end on byte boundary */
  866. if ((nbits + bit_offset) % BITS_PER_BYTE) {
  867. /* setup the last byte with msb bits from nvmem */
  868. rc = nvmem_reg_read(nvmem,
  869. cell->offset + cell->bytes - 1, &v, 1);
  870. *p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
  871. }
  872. return buf;
  873. }
  874. /**
  875. * nvmem_cell_write() - Write to a given nvmem cell
  876. *
  877. * @cell: nvmem cell to be written.
  878. * @buf: Buffer to be written.
  879. * @len: length of buffer to be written to nvmem cell.
  880. *
  881. * Return: length of bytes written or negative on failure.
  882. */
  883. int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
  884. {
  885. struct nvmem_device *nvmem = cell->nvmem;
  886. int rc;
  887. if (!nvmem || nvmem->read_only ||
  888. (cell->bit_offset == 0 && len != cell->bytes))
  889. return -EINVAL;
  890. if (cell->bit_offset || cell->nbits) {
  891. buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
  892. if (IS_ERR(buf))
  893. return PTR_ERR(buf);
  894. }
  895. rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
  896. /* free the tmp buffer */
  897. if (cell->bit_offset || cell->nbits)
  898. kfree(buf);
  899. if (rc)
  900. return rc;
  901. return len;
  902. }
  903. EXPORT_SYMBOL_GPL(nvmem_cell_write);
  904. /**
  905. * nvmem_cell_read_u32() - Read a cell value as an u32
  906. *
  907. * @dev: Device that requests the nvmem cell.
  908. * @cell_id: Name of nvmem cell to read.
  909. * @val: pointer to output value.
  910. *
  911. * Return: 0 on success or negative errno.
  912. */
  913. int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val)
  914. {
  915. struct nvmem_cell *cell;
  916. void *buf;
  917. size_t len;
  918. cell = nvmem_cell_get(dev, cell_id);
  919. if (IS_ERR(cell))
  920. return PTR_ERR(cell);
  921. buf = nvmem_cell_read(cell, &len);
  922. if (IS_ERR(buf)) {
  923. nvmem_cell_put(cell);
  924. return PTR_ERR(buf);
  925. }
  926. if (len != sizeof(*val)) {
  927. kfree(buf);
  928. nvmem_cell_put(cell);
  929. return -EINVAL;
  930. }
  931. memcpy(val, buf, sizeof(*val));
  932. kfree(buf);
  933. nvmem_cell_put(cell);
  934. return 0;
  935. }
  936. EXPORT_SYMBOL_GPL(nvmem_cell_read_u32);
  937. /**
  938. * nvmem_device_cell_read() - Read a given nvmem device and cell
  939. *
  940. * @nvmem: nvmem device to read from.
  941. * @info: nvmem cell info to be read.
  942. * @buf: buffer pointer which will be populated on successful read.
  943. *
  944. * Return: length of successful bytes read on success and negative
  945. * error code on error.
  946. */
  947. ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
  948. struct nvmem_cell_info *info, void *buf)
  949. {
  950. struct nvmem_cell cell;
  951. int rc;
  952. ssize_t len;
  953. if (!nvmem)
  954. return -EINVAL;
  955. rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell);
  956. if (rc)
  957. return rc;
  958. rc = __nvmem_cell_read(nvmem, &cell, buf, &len);
  959. if (rc)
  960. return rc;
  961. return len;
  962. }
  963. EXPORT_SYMBOL_GPL(nvmem_device_cell_read);
  964. /**
  965. * nvmem_device_cell_write() - Write cell to a given nvmem device
  966. *
  967. * @nvmem: nvmem device to be written to.
  968. * @info: nvmem cell info to be written.
  969. * @buf: buffer to be written to cell.
  970. *
  971. * Return: length of bytes written or negative error code on failure.
  972. * */
  973. int nvmem_device_cell_write(struct nvmem_device *nvmem,
  974. struct nvmem_cell_info *info, void *buf)
  975. {
  976. struct nvmem_cell cell;
  977. int rc;
  978. if (!nvmem)
  979. return -EINVAL;
  980. rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell);
  981. if (rc)
  982. return rc;
  983. return nvmem_cell_write(&cell, buf, cell.bytes);
  984. }
  985. EXPORT_SYMBOL_GPL(nvmem_device_cell_write);
  986. /**
  987. * nvmem_device_read() - Read from a given nvmem device
  988. *
  989. * @nvmem: nvmem device to read from.
  990. * @offset: offset in nvmem device.
  991. * @bytes: number of bytes to read.
  992. * @buf: buffer pointer which will be populated on successful read.
  993. *
  994. * Return: length of successful bytes read on success and negative
  995. * error code on error.
  996. */
  997. int nvmem_device_read(struct nvmem_device *nvmem,
  998. unsigned int offset,
  999. size_t bytes, void *buf)
  1000. {
  1001. int rc;
  1002. if (!nvmem)
  1003. return -EINVAL;
  1004. rc = nvmem_reg_read(nvmem, offset, buf, bytes);
  1005. if (rc)
  1006. return rc;
  1007. return bytes;
  1008. }
  1009. EXPORT_SYMBOL_GPL(nvmem_device_read);
  1010. /**
  1011. * nvmem_device_write() - Write cell to a given nvmem device
  1012. *
  1013. * @nvmem: nvmem device to be written to.
  1014. * @offset: offset in nvmem device.
  1015. * @bytes: number of bytes to write.
  1016. * @buf: buffer to be written.
  1017. *
  1018. * Return: length of bytes written or negative error code on failure.
  1019. * */
  1020. int nvmem_device_write(struct nvmem_device *nvmem,
  1021. unsigned int offset,
  1022. size_t bytes, void *buf)
  1023. {
  1024. int rc;
  1025. if (!nvmem)
  1026. return -EINVAL;
  1027. rc = nvmem_reg_write(nvmem, offset, buf, bytes);
  1028. if (rc)
  1029. return rc;
  1030. return bytes;
  1031. }
  1032. EXPORT_SYMBOL_GPL(nvmem_device_write);
  1033. static int __init nvmem_init(void)
  1034. {
  1035. return bus_register(&nvmem_bus_type);
  1036. }
  1037. static void __exit nvmem_exit(void)
  1038. {
  1039. bus_unregister(&nvmem_bus_type);
  1040. }
  1041. subsys_initcall(nvmem_init);
  1042. module_exit(nvmem_exit);
  1043. MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
  1044. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
  1045. MODULE_DESCRIPTION("nvmem Driver Core");
  1046. MODULE_LICENSE("GPL v2");