core.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  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. int rval;
  362. if (!config->dev)
  363. return ERR_PTR(-EINVAL);
  364. nvmem = kzalloc(sizeof(*nvmem), GFP_KERNEL);
  365. if (!nvmem)
  366. return ERR_PTR(-ENOMEM);
  367. rval = ida_simple_get(&nvmem_ida, 0, 0, GFP_KERNEL);
  368. if (rval < 0) {
  369. kfree(nvmem);
  370. return ERR_PTR(rval);
  371. }
  372. nvmem->id = rval;
  373. nvmem->owner = config->owner;
  374. if (!nvmem->owner && config->dev->driver)
  375. nvmem->owner = config->dev->driver->owner;
  376. nvmem->stride = config->stride ?: 1;
  377. nvmem->word_size = config->word_size ?: 1;
  378. nvmem->size = config->size;
  379. nvmem->dev.type = &nvmem_provider_type;
  380. nvmem->dev.bus = &nvmem_bus_type;
  381. nvmem->dev.parent = config->dev;
  382. nvmem->priv = config->priv;
  383. nvmem->reg_read = config->reg_read;
  384. nvmem->reg_write = config->reg_write;
  385. nvmem->dev.of_node = config->dev->of_node;
  386. if (config->id == -1 && config->name) {
  387. dev_set_name(&nvmem->dev, "%s", config->name);
  388. } else {
  389. dev_set_name(&nvmem->dev, "%s%d",
  390. config->name ? : "nvmem",
  391. config->name ? config->id : nvmem->id);
  392. }
  393. nvmem->read_only = device_property_present(config->dev, "read-only") |
  394. config->read_only;
  395. if (config->root_only)
  396. nvmem->dev.groups = nvmem->read_only ?
  397. nvmem_ro_root_dev_groups :
  398. nvmem_rw_root_dev_groups;
  399. else
  400. nvmem->dev.groups = nvmem->read_only ?
  401. nvmem_ro_dev_groups :
  402. nvmem_rw_dev_groups;
  403. device_initialize(&nvmem->dev);
  404. dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
  405. rval = device_add(&nvmem->dev);
  406. if (rval)
  407. goto err_put_device;
  408. if (config->compat) {
  409. rval = nvmem_setup_compat(nvmem, config);
  410. if (rval)
  411. goto err_device_del;
  412. }
  413. if (config->cells)
  414. nvmem_add_cells(nvmem, config);
  415. return nvmem;
  416. err_device_del:
  417. device_del(&nvmem->dev);
  418. err_put_device:
  419. put_device(&nvmem->dev);
  420. return ERR_PTR(rval);
  421. }
  422. EXPORT_SYMBOL_GPL(nvmem_register);
  423. /**
  424. * nvmem_unregister() - Unregister previously registered nvmem device
  425. *
  426. * @nvmem: Pointer to previously registered nvmem device.
  427. *
  428. * Return: Will be an negative on error or a zero on success.
  429. */
  430. int nvmem_unregister(struct nvmem_device *nvmem)
  431. {
  432. mutex_lock(&nvmem_mutex);
  433. if (nvmem->users) {
  434. mutex_unlock(&nvmem_mutex);
  435. return -EBUSY;
  436. }
  437. mutex_unlock(&nvmem_mutex);
  438. if (nvmem->flags & FLAG_COMPAT)
  439. device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
  440. nvmem_device_remove_all_cells(nvmem);
  441. device_del(&nvmem->dev);
  442. put_device(&nvmem->dev);
  443. return 0;
  444. }
  445. EXPORT_SYMBOL_GPL(nvmem_unregister);
  446. static void devm_nvmem_release(struct device *dev, void *res)
  447. {
  448. WARN_ON(nvmem_unregister(*(struct nvmem_device **)res));
  449. }
  450. /**
  451. * devm_nvmem_register() - Register a managed nvmem device for given
  452. * nvmem_config.
  453. * Also creates an binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
  454. *
  455. * @config: nvmem device configuration with which nvmem device is created.
  456. *
  457. * Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
  458. * on success.
  459. */
  460. struct nvmem_device *devm_nvmem_register(struct device *dev,
  461. const struct nvmem_config *config)
  462. {
  463. struct nvmem_device **ptr, *nvmem;
  464. ptr = devres_alloc(devm_nvmem_release, sizeof(*ptr), GFP_KERNEL);
  465. if (!ptr)
  466. return ERR_PTR(-ENOMEM);
  467. nvmem = nvmem_register(config);
  468. if (!IS_ERR(nvmem)) {
  469. *ptr = nvmem;
  470. devres_add(dev, ptr);
  471. } else {
  472. devres_free(ptr);
  473. }
  474. return nvmem;
  475. }
  476. EXPORT_SYMBOL_GPL(devm_nvmem_register);
  477. static int devm_nvmem_match(struct device *dev, void *res, void *data)
  478. {
  479. struct nvmem_device **r = res;
  480. return *r == data;
  481. }
  482. /**
  483. * devm_nvmem_unregister() - Unregister previously registered managed nvmem
  484. * device.
  485. *
  486. * @nvmem: Pointer to previously registered nvmem device.
  487. *
  488. * Return: Will be an negative on error or a zero on success.
  489. */
  490. int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem)
  491. {
  492. return devres_release(dev, devm_nvmem_release, devm_nvmem_match, nvmem);
  493. }
  494. EXPORT_SYMBOL(devm_nvmem_unregister);
  495. static struct nvmem_device *__nvmem_device_get(struct device_node *np,
  496. struct nvmem_cell **cellp,
  497. const char *cell_id)
  498. {
  499. struct nvmem_device *nvmem = NULL;
  500. mutex_lock(&nvmem_mutex);
  501. if (np) {
  502. nvmem = of_nvmem_find(np);
  503. if (!nvmem) {
  504. mutex_unlock(&nvmem_mutex);
  505. return ERR_PTR(-EPROBE_DEFER);
  506. }
  507. } else {
  508. struct nvmem_cell *cell = nvmem_find_cell(cell_id);
  509. if (cell) {
  510. nvmem = cell->nvmem;
  511. *cellp = cell;
  512. }
  513. if (!nvmem) {
  514. mutex_unlock(&nvmem_mutex);
  515. return ERR_PTR(-ENOENT);
  516. }
  517. }
  518. nvmem->users++;
  519. mutex_unlock(&nvmem_mutex);
  520. if (!try_module_get(nvmem->owner)) {
  521. dev_err(&nvmem->dev,
  522. "could not increase module refcount for cell %s\n",
  523. nvmem->name);
  524. mutex_lock(&nvmem_mutex);
  525. nvmem->users--;
  526. mutex_unlock(&nvmem_mutex);
  527. return ERR_PTR(-EINVAL);
  528. }
  529. return nvmem;
  530. }
  531. static void __nvmem_device_put(struct nvmem_device *nvmem)
  532. {
  533. module_put(nvmem->owner);
  534. mutex_lock(&nvmem_mutex);
  535. nvmem->users--;
  536. mutex_unlock(&nvmem_mutex);
  537. }
  538. static struct nvmem_device *nvmem_find(const char *name)
  539. {
  540. struct device *d;
  541. d = bus_find_device_by_name(&nvmem_bus_type, NULL, name);
  542. if (!d)
  543. return NULL;
  544. return to_nvmem_device(d);
  545. }
  546. #if IS_ENABLED(CONFIG_OF)
  547. /**
  548. * of_nvmem_device_get() - Get nvmem device from a given id
  549. *
  550. * @np: Device tree node that uses the nvmem device.
  551. * @id: nvmem name from nvmem-names property.
  552. *
  553. * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
  554. * on success.
  555. */
  556. struct nvmem_device *of_nvmem_device_get(struct device_node *np, const char *id)
  557. {
  558. struct device_node *nvmem_np;
  559. int index;
  560. index = of_property_match_string(np, "nvmem-names", id);
  561. nvmem_np = of_parse_phandle(np, "nvmem", index);
  562. if (!nvmem_np)
  563. return ERR_PTR(-EINVAL);
  564. return __nvmem_device_get(nvmem_np, NULL, NULL);
  565. }
  566. EXPORT_SYMBOL_GPL(of_nvmem_device_get);
  567. #endif
  568. /**
  569. * nvmem_device_get() - Get nvmem device from a given id
  570. *
  571. * @dev: Device that uses the nvmem device.
  572. * @dev_name: name of the requested nvmem device.
  573. *
  574. * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_device
  575. * on success.
  576. */
  577. struct nvmem_device *nvmem_device_get(struct device *dev, const char *dev_name)
  578. {
  579. if (dev->of_node) { /* try dt first */
  580. struct nvmem_device *nvmem;
  581. nvmem = of_nvmem_device_get(dev->of_node, dev_name);
  582. if (!IS_ERR(nvmem) || PTR_ERR(nvmem) == -EPROBE_DEFER)
  583. return nvmem;
  584. }
  585. return nvmem_find(dev_name);
  586. }
  587. EXPORT_SYMBOL_GPL(nvmem_device_get);
  588. static int devm_nvmem_device_match(struct device *dev, void *res, void *data)
  589. {
  590. struct nvmem_device **nvmem = res;
  591. if (WARN_ON(!nvmem || !*nvmem))
  592. return 0;
  593. return *nvmem == data;
  594. }
  595. static void devm_nvmem_device_release(struct device *dev, void *res)
  596. {
  597. nvmem_device_put(*(struct nvmem_device **)res);
  598. }
  599. /**
  600. * devm_nvmem_device_put() - put alredy got nvmem device
  601. *
  602. * @dev: Device that uses the nvmem device.
  603. * @nvmem: pointer to nvmem device allocated by devm_nvmem_cell_get(),
  604. * that needs to be released.
  605. */
  606. void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem)
  607. {
  608. int ret;
  609. ret = devres_release(dev, devm_nvmem_device_release,
  610. devm_nvmem_device_match, nvmem);
  611. WARN_ON(ret);
  612. }
  613. EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
  614. /**
  615. * nvmem_device_put() - put alredy got nvmem device
  616. *
  617. * @nvmem: pointer to nvmem device that needs to be released.
  618. */
  619. void nvmem_device_put(struct nvmem_device *nvmem)
  620. {
  621. __nvmem_device_put(nvmem);
  622. }
  623. EXPORT_SYMBOL_GPL(nvmem_device_put);
  624. /**
  625. * devm_nvmem_device_get() - Get nvmem cell of device form a given id
  626. *
  627. * @dev: Device that requests the nvmem device.
  628. * @id: name id for the requested nvmem device.
  629. *
  630. * Return: ERR_PTR() on error or a valid pointer to a struct nvmem_cell
  631. * on success. The nvmem_cell will be freed by the automatically once the
  632. * device is freed.
  633. */
  634. struct nvmem_device *devm_nvmem_device_get(struct device *dev, const char *id)
  635. {
  636. struct nvmem_device **ptr, *nvmem;
  637. ptr = devres_alloc(devm_nvmem_device_release, sizeof(*ptr), GFP_KERNEL);
  638. if (!ptr)
  639. return ERR_PTR(-ENOMEM);
  640. nvmem = nvmem_device_get(dev, id);
  641. if (!IS_ERR(nvmem)) {
  642. *ptr = nvmem;
  643. devres_add(dev, ptr);
  644. } else {
  645. devres_free(ptr);
  646. }
  647. return nvmem;
  648. }
  649. EXPORT_SYMBOL_GPL(devm_nvmem_device_get);
  650. static struct nvmem_cell *nvmem_cell_get_from_list(const char *cell_id)
  651. {
  652. struct nvmem_cell *cell = NULL;
  653. struct nvmem_device *nvmem;
  654. nvmem = __nvmem_device_get(NULL, &cell, cell_id);
  655. if (IS_ERR(nvmem))
  656. return ERR_CAST(nvmem);
  657. return cell;
  658. }
  659. #if IS_ENABLED(CONFIG_OF)
  660. /**
  661. * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
  662. *
  663. * @np: Device tree node that uses the nvmem cell.
  664. * @name: nvmem cell name from nvmem-cell-names property, or NULL
  665. * for the cell at index 0 (the lone cell with no accompanying
  666. * nvmem-cell-names property).
  667. *
  668. * Return: Will be an ERR_PTR() on error or a valid pointer
  669. * to a struct nvmem_cell. The nvmem_cell will be freed by the
  670. * nvmem_cell_put().
  671. */
  672. struct nvmem_cell *of_nvmem_cell_get(struct device_node *np,
  673. const char *name)
  674. {
  675. struct device_node *cell_np, *nvmem_np;
  676. struct nvmem_cell *cell;
  677. struct nvmem_device *nvmem;
  678. const __be32 *addr;
  679. int rval, len;
  680. int index = 0;
  681. /* if cell name exists, find index to the name */
  682. if (name)
  683. index = of_property_match_string(np, "nvmem-cell-names", name);
  684. cell_np = of_parse_phandle(np, "nvmem-cells", index);
  685. if (!cell_np)
  686. return ERR_PTR(-EINVAL);
  687. nvmem_np = of_get_next_parent(cell_np);
  688. if (!nvmem_np)
  689. return ERR_PTR(-EINVAL);
  690. nvmem = __nvmem_device_get(nvmem_np, NULL, NULL);
  691. of_node_put(nvmem_np);
  692. if (IS_ERR(nvmem))
  693. return ERR_CAST(nvmem);
  694. addr = of_get_property(cell_np, "reg", &len);
  695. if (!addr || (len < 2 * sizeof(u32))) {
  696. dev_err(&nvmem->dev, "nvmem: invalid reg on %pOF\n",
  697. cell_np);
  698. rval = -EINVAL;
  699. goto err_mem;
  700. }
  701. cell = kzalloc(sizeof(*cell), GFP_KERNEL);
  702. if (!cell) {
  703. rval = -ENOMEM;
  704. goto err_mem;
  705. }
  706. cell->nvmem = nvmem;
  707. cell->offset = be32_to_cpup(addr++);
  708. cell->bytes = be32_to_cpup(addr);
  709. cell->name = cell_np->name;
  710. addr = of_get_property(cell_np, "bits", &len);
  711. if (addr && len == (2 * sizeof(u32))) {
  712. cell->bit_offset = be32_to_cpup(addr++);
  713. cell->nbits = be32_to_cpup(addr);
  714. }
  715. if (cell->nbits)
  716. cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset,
  717. BITS_PER_BYTE);
  718. if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
  719. dev_err(&nvmem->dev,
  720. "cell %s unaligned to nvmem stride %d\n",
  721. cell->name, nvmem->stride);
  722. rval = -EINVAL;
  723. goto err_sanity;
  724. }
  725. nvmem_cell_add(cell);
  726. return cell;
  727. err_sanity:
  728. kfree(cell);
  729. err_mem:
  730. __nvmem_device_put(nvmem);
  731. return ERR_PTR(rval);
  732. }
  733. EXPORT_SYMBOL_GPL(of_nvmem_cell_get);
  734. #endif
  735. /**
  736. * nvmem_cell_get() - Get nvmem cell of device form a given cell name
  737. *
  738. * @dev: Device that requests the nvmem cell.
  739. * @cell_id: nvmem cell name to get.
  740. *
  741. * Return: Will be an ERR_PTR() on error or a valid pointer
  742. * to a struct nvmem_cell. The nvmem_cell will be freed by the
  743. * nvmem_cell_put().
  744. */
  745. struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *cell_id)
  746. {
  747. struct nvmem_cell *cell;
  748. if (dev->of_node) { /* try dt first */
  749. cell = of_nvmem_cell_get(dev->of_node, cell_id);
  750. if (!IS_ERR(cell) || PTR_ERR(cell) == -EPROBE_DEFER)
  751. return cell;
  752. }
  753. return nvmem_cell_get_from_list(cell_id);
  754. }
  755. EXPORT_SYMBOL_GPL(nvmem_cell_get);
  756. static void devm_nvmem_cell_release(struct device *dev, void *res)
  757. {
  758. nvmem_cell_put(*(struct nvmem_cell **)res);
  759. }
  760. /**
  761. * devm_nvmem_cell_get() - Get nvmem cell of device form a given id
  762. *
  763. * @dev: Device that requests the nvmem cell.
  764. * @id: nvmem cell name id to get.
  765. *
  766. * Return: Will be an ERR_PTR() on error or a valid pointer
  767. * to a struct nvmem_cell. The nvmem_cell will be freed by the
  768. * automatically once the device is freed.
  769. */
  770. struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *id)
  771. {
  772. struct nvmem_cell **ptr, *cell;
  773. ptr = devres_alloc(devm_nvmem_cell_release, sizeof(*ptr), GFP_KERNEL);
  774. if (!ptr)
  775. return ERR_PTR(-ENOMEM);
  776. cell = nvmem_cell_get(dev, id);
  777. if (!IS_ERR(cell)) {
  778. *ptr = cell;
  779. devres_add(dev, ptr);
  780. } else {
  781. devres_free(ptr);
  782. }
  783. return cell;
  784. }
  785. EXPORT_SYMBOL_GPL(devm_nvmem_cell_get);
  786. static int devm_nvmem_cell_match(struct device *dev, void *res, void *data)
  787. {
  788. struct nvmem_cell **c = res;
  789. if (WARN_ON(!c || !*c))
  790. return 0;
  791. return *c == data;
  792. }
  793. /**
  794. * devm_nvmem_cell_put() - Release previously allocated nvmem cell
  795. * from devm_nvmem_cell_get.
  796. *
  797. * @dev: Device that requests the nvmem cell.
  798. * @cell: Previously allocated nvmem cell by devm_nvmem_cell_get().
  799. */
  800. void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell)
  801. {
  802. int ret;
  803. ret = devres_release(dev, devm_nvmem_cell_release,
  804. devm_nvmem_cell_match, cell);
  805. WARN_ON(ret);
  806. }
  807. EXPORT_SYMBOL(devm_nvmem_cell_put);
  808. /**
  809. * nvmem_cell_put() - Release previously allocated nvmem cell.
  810. *
  811. * @cell: Previously allocated nvmem cell by nvmem_cell_get().
  812. */
  813. void nvmem_cell_put(struct nvmem_cell *cell)
  814. {
  815. struct nvmem_device *nvmem = cell->nvmem;
  816. __nvmem_device_put(nvmem);
  817. nvmem_cell_drop(cell);
  818. }
  819. EXPORT_SYMBOL_GPL(nvmem_cell_put);
  820. static void nvmem_shift_read_buffer_in_place(struct nvmem_cell *cell, void *buf)
  821. {
  822. u8 *p, *b;
  823. int i, bit_offset = cell->bit_offset;
  824. p = b = buf;
  825. if (bit_offset) {
  826. /* First shift */
  827. *b++ >>= bit_offset;
  828. /* setup rest of the bytes if any */
  829. for (i = 1; i < cell->bytes; i++) {
  830. /* Get bits from next byte and shift them towards msb */
  831. *p |= *b << (BITS_PER_BYTE - bit_offset);
  832. p = b;
  833. *b++ >>= bit_offset;
  834. }
  835. /* result fits in less bytes */
  836. if (cell->bytes != DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE))
  837. *p-- = 0;
  838. }
  839. /* clear msb bits if any leftover in the last byte */
  840. *p &= GENMASK((cell->nbits%BITS_PER_BYTE) - 1, 0);
  841. }
  842. static int __nvmem_cell_read(struct nvmem_device *nvmem,
  843. struct nvmem_cell *cell,
  844. void *buf, size_t *len)
  845. {
  846. int rc;
  847. rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->bytes);
  848. if (rc)
  849. return rc;
  850. /* shift bits in-place */
  851. if (cell->bit_offset || cell->nbits)
  852. nvmem_shift_read_buffer_in_place(cell, buf);
  853. if (len)
  854. *len = cell->bytes;
  855. return 0;
  856. }
  857. /**
  858. * nvmem_cell_read() - Read a given nvmem cell
  859. *
  860. * @cell: nvmem cell to be read.
  861. * @len: pointer to length of cell which will be populated on successful read;
  862. * can be NULL.
  863. *
  864. * Return: ERR_PTR() on error or a valid pointer to a buffer on success. The
  865. * buffer should be freed by the consumer with a kfree().
  866. */
  867. void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
  868. {
  869. struct nvmem_device *nvmem = cell->nvmem;
  870. u8 *buf;
  871. int rc;
  872. if (!nvmem)
  873. return ERR_PTR(-EINVAL);
  874. buf = kzalloc(cell->bytes, GFP_KERNEL);
  875. if (!buf)
  876. return ERR_PTR(-ENOMEM);
  877. rc = __nvmem_cell_read(nvmem, cell, buf, len);
  878. if (rc) {
  879. kfree(buf);
  880. return ERR_PTR(rc);
  881. }
  882. return buf;
  883. }
  884. EXPORT_SYMBOL_GPL(nvmem_cell_read);
  885. static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell *cell,
  886. u8 *_buf, int len)
  887. {
  888. struct nvmem_device *nvmem = cell->nvmem;
  889. int i, rc, nbits, bit_offset = cell->bit_offset;
  890. u8 v, *p, *buf, *b, pbyte, pbits;
  891. nbits = cell->nbits;
  892. buf = kzalloc(cell->bytes, GFP_KERNEL);
  893. if (!buf)
  894. return ERR_PTR(-ENOMEM);
  895. memcpy(buf, _buf, len);
  896. p = b = buf;
  897. if (bit_offset) {
  898. pbyte = *b;
  899. *b <<= bit_offset;
  900. /* setup the first byte with lsb bits from nvmem */
  901. rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
  902. *b++ |= GENMASK(bit_offset - 1, 0) & v;
  903. /* setup rest of the byte if any */
  904. for (i = 1; i < cell->bytes; i++) {
  905. /* Get last byte bits and shift them towards lsb */
  906. pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset);
  907. pbyte = *b;
  908. p = b;
  909. *b <<= bit_offset;
  910. *b++ |= pbits;
  911. }
  912. }
  913. /* if it's not end on byte boundary */
  914. if ((nbits + bit_offset) % BITS_PER_BYTE) {
  915. /* setup the last byte with msb bits from nvmem */
  916. rc = nvmem_reg_read(nvmem,
  917. cell->offset + cell->bytes - 1, &v, 1);
  918. *p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
  919. }
  920. return buf;
  921. }
  922. /**
  923. * nvmem_cell_write() - Write to a given nvmem cell
  924. *
  925. * @cell: nvmem cell to be written.
  926. * @buf: Buffer to be written.
  927. * @len: length of buffer to be written to nvmem cell.
  928. *
  929. * Return: length of bytes written or negative on failure.
  930. */
  931. int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
  932. {
  933. struct nvmem_device *nvmem = cell->nvmem;
  934. int rc;
  935. if (!nvmem || nvmem->read_only ||
  936. (cell->bit_offset == 0 && len != cell->bytes))
  937. return -EINVAL;
  938. if (cell->bit_offset || cell->nbits) {
  939. buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
  940. if (IS_ERR(buf))
  941. return PTR_ERR(buf);
  942. }
  943. rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
  944. /* free the tmp buffer */
  945. if (cell->bit_offset || cell->nbits)
  946. kfree(buf);
  947. if (rc)
  948. return rc;
  949. return len;
  950. }
  951. EXPORT_SYMBOL_GPL(nvmem_cell_write);
  952. /**
  953. * nvmem_cell_read_u32() - Read a cell value as an u32
  954. *
  955. * @dev: Device that requests the nvmem cell.
  956. * @cell_id: Name of nvmem cell to read.
  957. * @val: pointer to output value.
  958. *
  959. * Return: 0 on success or negative errno.
  960. */
  961. int nvmem_cell_read_u32(struct device *dev, const char *cell_id, u32 *val)
  962. {
  963. struct nvmem_cell *cell;
  964. void *buf;
  965. size_t len;
  966. cell = nvmem_cell_get(dev, cell_id);
  967. if (IS_ERR(cell))
  968. return PTR_ERR(cell);
  969. buf = nvmem_cell_read(cell, &len);
  970. if (IS_ERR(buf)) {
  971. nvmem_cell_put(cell);
  972. return PTR_ERR(buf);
  973. }
  974. if (len != sizeof(*val)) {
  975. kfree(buf);
  976. nvmem_cell_put(cell);
  977. return -EINVAL;
  978. }
  979. memcpy(val, buf, sizeof(*val));
  980. kfree(buf);
  981. nvmem_cell_put(cell);
  982. return 0;
  983. }
  984. EXPORT_SYMBOL_GPL(nvmem_cell_read_u32);
  985. /**
  986. * nvmem_device_cell_read() - Read a given nvmem device and cell
  987. *
  988. * @nvmem: nvmem device to read from.
  989. * @info: nvmem cell info to be read.
  990. * @buf: buffer pointer which will be populated on successful read.
  991. *
  992. * Return: length of successful bytes read on success and negative
  993. * error code on error.
  994. */
  995. ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
  996. struct nvmem_cell_info *info, void *buf)
  997. {
  998. struct nvmem_cell cell;
  999. int rc;
  1000. ssize_t len;
  1001. if (!nvmem)
  1002. return -EINVAL;
  1003. rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell);
  1004. if (rc)
  1005. return rc;
  1006. rc = __nvmem_cell_read(nvmem, &cell, buf, &len);
  1007. if (rc)
  1008. return rc;
  1009. return len;
  1010. }
  1011. EXPORT_SYMBOL_GPL(nvmem_device_cell_read);
  1012. /**
  1013. * nvmem_device_cell_write() - Write cell to a given nvmem device
  1014. *
  1015. * @nvmem: nvmem device to be written to.
  1016. * @info: nvmem cell info to be written.
  1017. * @buf: buffer to be written to cell.
  1018. *
  1019. * Return: length of bytes written or negative error code on failure.
  1020. * */
  1021. int nvmem_device_cell_write(struct nvmem_device *nvmem,
  1022. struct nvmem_cell_info *info, void *buf)
  1023. {
  1024. struct nvmem_cell cell;
  1025. int rc;
  1026. if (!nvmem)
  1027. return -EINVAL;
  1028. rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell);
  1029. if (rc)
  1030. return rc;
  1031. return nvmem_cell_write(&cell, buf, cell.bytes);
  1032. }
  1033. EXPORT_SYMBOL_GPL(nvmem_device_cell_write);
  1034. /**
  1035. * nvmem_device_read() - Read from a given nvmem device
  1036. *
  1037. * @nvmem: nvmem device to read from.
  1038. * @offset: offset in nvmem device.
  1039. * @bytes: number of bytes to read.
  1040. * @buf: buffer pointer which will be populated on successful read.
  1041. *
  1042. * Return: length of successful bytes read on success and negative
  1043. * error code on error.
  1044. */
  1045. int nvmem_device_read(struct nvmem_device *nvmem,
  1046. unsigned int offset,
  1047. size_t bytes, void *buf)
  1048. {
  1049. int rc;
  1050. if (!nvmem)
  1051. return -EINVAL;
  1052. rc = nvmem_reg_read(nvmem, offset, buf, bytes);
  1053. if (rc)
  1054. return rc;
  1055. return bytes;
  1056. }
  1057. EXPORT_SYMBOL_GPL(nvmem_device_read);
  1058. /**
  1059. * nvmem_device_write() - Write cell to a given nvmem device
  1060. *
  1061. * @nvmem: nvmem device to be written to.
  1062. * @offset: offset in nvmem device.
  1063. * @bytes: number of bytes to write.
  1064. * @buf: buffer to be written.
  1065. *
  1066. * Return: length of bytes written or negative error code on failure.
  1067. * */
  1068. int nvmem_device_write(struct nvmem_device *nvmem,
  1069. unsigned int offset,
  1070. size_t bytes, void *buf)
  1071. {
  1072. int rc;
  1073. if (!nvmem)
  1074. return -EINVAL;
  1075. rc = nvmem_reg_write(nvmem, offset, buf, bytes);
  1076. if (rc)
  1077. return rc;
  1078. return bytes;
  1079. }
  1080. EXPORT_SYMBOL_GPL(nvmem_device_write);
  1081. static int __init nvmem_init(void)
  1082. {
  1083. return bus_register(&nvmem_bus_type);
  1084. }
  1085. static void __exit nvmem_exit(void)
  1086. {
  1087. bus_unregister(&nvmem_bus_type);
  1088. }
  1089. subsys_initcall(nvmem_init);
  1090. module_exit(nvmem_exit);
  1091. MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
  1092. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com");
  1093. MODULE_DESCRIPTION("nvmem Driver Core");
  1094. MODULE_LICENSE("GPL v2");