framer-core.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Generic Framer framework.
  4. *
  5. * Copyright 2023 CS GROUP France
  6. *
  7. * Author: Herve Codina <herve.codina@bootlin.com>
  8. */
  9. #include <linux/device.h>
  10. #include <linux/framer/framer.h>
  11. #include <linux/framer/framer-provider.h>
  12. #include <linux/idr.h>
  13. #include <linux/module.h>
  14. #include <linux/notifier.h>
  15. #include <linux/of.h>
  16. #include <linux/pm_runtime.h>
  17. #include <linux/regulator/consumer.h>
  18. #include <linux/slab.h>
  19. static void framer_release(struct device *dev);
  20. static const struct class framer_class = {
  21. .name = "framer",
  22. .dev_release = framer_release,
  23. };
  24. static DEFINE_MUTEX(framer_provider_mutex);
  25. static LIST_HEAD(framer_provider_list);
  26. static DEFINE_IDA(framer_ida);
  27. #define dev_to_framer(a) (container_of((a), struct framer, dev))
  28. int framer_pm_runtime_get(struct framer *framer)
  29. {
  30. int ret;
  31. if (!pm_runtime_enabled(&framer->dev))
  32. return -EOPNOTSUPP;
  33. ret = pm_runtime_get(&framer->dev);
  34. if (ret < 0 && ret != -EINPROGRESS)
  35. pm_runtime_put_noidle(&framer->dev);
  36. return ret;
  37. }
  38. EXPORT_SYMBOL_GPL(framer_pm_runtime_get);
  39. int framer_pm_runtime_get_sync(struct framer *framer)
  40. {
  41. int ret;
  42. if (!pm_runtime_enabled(&framer->dev))
  43. return -EOPNOTSUPP;
  44. ret = pm_runtime_get_sync(&framer->dev);
  45. if (ret < 0)
  46. pm_runtime_put_sync(&framer->dev);
  47. return ret;
  48. }
  49. EXPORT_SYMBOL_GPL(framer_pm_runtime_get_sync);
  50. void framer_pm_runtime_put(struct framer *framer)
  51. {
  52. if (!pm_runtime_enabled(&framer->dev))
  53. return;
  54. pm_runtime_put(&framer->dev);
  55. }
  56. EXPORT_SYMBOL_GPL(framer_pm_runtime_put);
  57. int framer_pm_runtime_put_sync(struct framer *framer)
  58. {
  59. if (!pm_runtime_enabled(&framer->dev))
  60. return -EOPNOTSUPP;
  61. return pm_runtime_put_sync(&framer->dev);
  62. }
  63. EXPORT_SYMBOL_GPL(framer_pm_runtime_put_sync);
  64. /**
  65. * framer_init - framer internal initialization before framer operation
  66. * @framer: the framer returned by framer_get()
  67. *
  68. * Used to allow framer's driver to perform framer internal initialization,
  69. * such as PLL block powering, clock initialization or anything that's
  70. * is required by the framer to perform the start of operation.
  71. * Must be called before framer_power_on().
  72. *
  73. * Return: %0 if successful, a negative error code otherwise
  74. */
  75. int framer_init(struct framer *framer)
  76. {
  77. bool start_polling = false;
  78. int ret;
  79. ret = framer_pm_runtime_get_sync(framer);
  80. if (ret < 0 && ret != -EOPNOTSUPP)
  81. return ret;
  82. ret = 0; /* Override possible ret == -EOPNOTSUPP */
  83. mutex_lock(&framer->mutex);
  84. if (framer->power_count > framer->init_count)
  85. dev_warn(&framer->dev, "framer_power_on was called before framer init\n");
  86. if (framer->init_count == 0) {
  87. if (framer->ops->init) {
  88. ret = framer->ops->init(framer);
  89. if (ret < 0) {
  90. dev_err(&framer->dev, "framer init failed --> %d\n", ret);
  91. goto out;
  92. }
  93. }
  94. if (framer->ops->flags & FRAMER_FLAG_POLL_STATUS)
  95. start_polling = true;
  96. }
  97. ++framer->init_count;
  98. out:
  99. mutex_unlock(&framer->mutex);
  100. if (!ret && start_polling) {
  101. ret = framer_get_status(framer, &framer->prev_status);
  102. if (ret < 0) {
  103. dev_warn(&framer->dev, "framer get status failed --> %d\n", ret);
  104. /* Will be retried on polling_work */
  105. ret = 0;
  106. }
  107. queue_delayed_work(system_power_efficient_wq, &framer->polling_work, 1 * HZ);
  108. }
  109. framer_pm_runtime_put(framer);
  110. return ret;
  111. }
  112. EXPORT_SYMBOL_GPL(framer_init);
  113. /**
  114. * framer_exit - Framer internal un-initialization
  115. * @framer: the framer returned by framer_get()
  116. *
  117. * Must be called after framer_power_off().
  118. */
  119. int framer_exit(struct framer *framer)
  120. {
  121. int ret;
  122. ret = framer_pm_runtime_get_sync(framer);
  123. if (ret < 0 && ret != -EOPNOTSUPP)
  124. return ret;
  125. ret = 0; /* Override possible ret == -EOPNOTSUPP */
  126. mutex_lock(&framer->mutex);
  127. --framer->init_count;
  128. if (framer->init_count == 0) {
  129. if (framer->ops->flags & FRAMER_FLAG_POLL_STATUS) {
  130. mutex_unlock(&framer->mutex);
  131. cancel_delayed_work_sync(&framer->polling_work);
  132. mutex_lock(&framer->mutex);
  133. }
  134. if (framer->ops->exit)
  135. framer->ops->exit(framer);
  136. }
  137. mutex_unlock(&framer->mutex);
  138. framer_pm_runtime_put(framer);
  139. return ret;
  140. }
  141. EXPORT_SYMBOL_GPL(framer_exit);
  142. /**
  143. * framer_power_on - Enable the framer and enter proper operation
  144. * @framer: the framer returned by framer_get()
  145. *
  146. * Must be called after framer_init().
  147. *
  148. * Return: %0 if successful, a negative error code otherwise
  149. */
  150. int framer_power_on(struct framer *framer)
  151. {
  152. int ret;
  153. if (framer->pwr) {
  154. ret = regulator_enable(framer->pwr);
  155. if (ret)
  156. return ret;
  157. }
  158. ret = framer_pm_runtime_get_sync(framer);
  159. if (ret < 0 && ret != -EOPNOTSUPP)
  160. goto err_pm_sync;
  161. mutex_lock(&framer->mutex);
  162. if (framer->power_count == 0 && framer->ops->power_on) {
  163. ret = framer->ops->power_on(framer);
  164. if (ret < 0) {
  165. dev_err(&framer->dev, "framer poweron failed --> %d\n", ret);
  166. goto err_pwr_on;
  167. }
  168. }
  169. ++framer->power_count;
  170. mutex_unlock(&framer->mutex);
  171. return 0;
  172. err_pwr_on:
  173. mutex_unlock(&framer->mutex);
  174. framer_pm_runtime_put_sync(framer);
  175. err_pm_sync:
  176. if (framer->pwr)
  177. regulator_disable(framer->pwr);
  178. return ret;
  179. }
  180. EXPORT_SYMBOL_GPL(framer_power_on);
  181. /**
  182. * framer_power_off - Disable the framer.
  183. * @framer: the framer returned by framer_get()
  184. *
  185. * Must be called before framer_exit().
  186. *
  187. * Return: %0 if successful, a negative error code otherwise
  188. */
  189. int framer_power_off(struct framer *framer)
  190. {
  191. int ret;
  192. mutex_lock(&framer->mutex);
  193. if (framer->power_count == 1 && framer->ops->power_off) {
  194. ret = framer->ops->power_off(framer);
  195. if (ret < 0) {
  196. dev_err(&framer->dev, "framer poweroff failed --> %d\n", ret);
  197. mutex_unlock(&framer->mutex);
  198. return ret;
  199. }
  200. }
  201. --framer->power_count;
  202. mutex_unlock(&framer->mutex);
  203. framer_pm_runtime_put(framer);
  204. if (framer->pwr)
  205. regulator_disable(framer->pwr);
  206. return 0;
  207. }
  208. EXPORT_SYMBOL_GPL(framer_power_off);
  209. /**
  210. * framer_get_status() - Gets the framer status
  211. * @framer: the framer returned by framer_get()
  212. * @status: the status to retrieve
  213. *
  214. * Used to get the framer status. framer_init() must have been called
  215. * on the framer.
  216. *
  217. * Return: %0 if successful, a negative error code otherwise
  218. */
  219. int framer_get_status(struct framer *framer, struct framer_status *status)
  220. {
  221. int ret;
  222. if (!framer->ops->get_status)
  223. return -EOPNOTSUPP;
  224. /* Be sure to have known values (struct padding and future extensions) */
  225. memset(status, 0, sizeof(*status));
  226. mutex_lock(&framer->mutex);
  227. ret = framer->ops->get_status(framer, status);
  228. mutex_unlock(&framer->mutex);
  229. return ret;
  230. }
  231. EXPORT_SYMBOL_GPL(framer_get_status);
  232. /**
  233. * framer_set_config() - Sets the framer configuration
  234. * @framer: the framer returned by framer_get()
  235. * @config: the configuration to set
  236. *
  237. * Used to set the framer configuration. framer_init() must have been called
  238. * on the framer.
  239. *
  240. * Return: %0 if successful, a negative error code otherwise
  241. */
  242. int framer_set_config(struct framer *framer, const struct framer_config *config)
  243. {
  244. int ret;
  245. if (!framer->ops->set_config)
  246. return -EOPNOTSUPP;
  247. mutex_lock(&framer->mutex);
  248. ret = framer->ops->set_config(framer, config);
  249. mutex_unlock(&framer->mutex);
  250. return ret;
  251. }
  252. EXPORT_SYMBOL_GPL(framer_set_config);
  253. /**
  254. * framer_get_config() - Gets the framer configuration
  255. * @framer: the framer returned by framer_get()
  256. * @config: the configuration to retrieve
  257. *
  258. * Used to get the framer configuration. framer_init() must have been called
  259. * on the framer.
  260. *
  261. * Return: %0 if successful, a negative error code otherwise
  262. */
  263. int framer_get_config(struct framer *framer, struct framer_config *config)
  264. {
  265. int ret;
  266. if (!framer->ops->get_config)
  267. return -EOPNOTSUPP;
  268. mutex_lock(&framer->mutex);
  269. ret = framer->ops->get_config(framer, config);
  270. mutex_unlock(&framer->mutex);
  271. return ret;
  272. }
  273. EXPORT_SYMBOL_GPL(framer_get_config);
  274. static void framer_polling_work(struct work_struct *work)
  275. {
  276. struct framer *framer = container_of(work, struct framer, polling_work.work);
  277. struct framer_status status;
  278. int ret;
  279. ret = framer_get_status(framer, &status);
  280. if (ret) {
  281. dev_err(&framer->dev, "polling, get status failed (%d)\n", ret);
  282. goto end;
  283. }
  284. if (memcmp(&framer->prev_status, &status, sizeof(status))) {
  285. blocking_notifier_call_chain(&framer->notifier_list,
  286. FRAMER_EVENT_STATUS, NULL);
  287. memcpy(&framer->prev_status, &status, sizeof(status));
  288. }
  289. end:
  290. /* Re-schedule task in 1 sec */
  291. queue_delayed_work(system_power_efficient_wq, &framer->polling_work, 1 * HZ);
  292. }
  293. /**
  294. * framer_notifier_register() - Registers a notifier
  295. * @framer: the framer returned by framer_get()
  296. * @nb: the notifier block to register
  297. *
  298. * Used to register a notifier block on framer events. framer_init() must have
  299. * been called on the framer.
  300. * The available framer events are present in enum framer_events.
  301. *
  302. * Return: %0 if successful, a negative error code otherwise
  303. */
  304. int framer_notifier_register(struct framer *framer, struct notifier_block *nb)
  305. {
  306. return blocking_notifier_chain_register(&framer->notifier_list, nb);
  307. }
  308. EXPORT_SYMBOL_GPL(framer_notifier_register);
  309. /**
  310. * framer_notifier_unregister() - Unregisters a notifier
  311. * @framer: the framer returned by framer_get()
  312. * @nb: the notifier block to unregister
  313. *
  314. * Used to unregister a notifier block. framer_init() must have
  315. * been called on the framer.
  316. *
  317. * Return: %0 if successful, a negative error code otherwise
  318. */
  319. int framer_notifier_unregister(struct framer *framer, struct notifier_block *nb)
  320. {
  321. return blocking_notifier_chain_unregister(&framer->notifier_list, nb);
  322. }
  323. EXPORT_SYMBOL_GPL(framer_notifier_unregister);
  324. static struct framer_provider *framer_provider_of_lookup(const struct device_node *node)
  325. {
  326. struct framer_provider *framer_provider;
  327. list_for_each_entry(framer_provider, &framer_provider_list, list) {
  328. if (device_match_of_node(framer_provider->dev, node))
  329. return framer_provider;
  330. }
  331. return ERR_PTR(-EPROBE_DEFER);
  332. }
  333. static struct framer *framer_of_get_from_provider(const struct of_phandle_args *args)
  334. {
  335. struct framer_provider *framer_provider;
  336. struct framer *framer;
  337. mutex_lock(&framer_provider_mutex);
  338. framer_provider = framer_provider_of_lookup(args->np);
  339. if (IS_ERR(framer_provider) || !try_module_get(framer_provider->owner)) {
  340. framer = ERR_PTR(-EPROBE_DEFER);
  341. goto end;
  342. }
  343. framer = framer_provider->of_xlate(framer_provider->dev, args);
  344. module_put(framer_provider->owner);
  345. end:
  346. mutex_unlock(&framer_provider_mutex);
  347. return framer;
  348. }
  349. static struct framer *framer_of_get_byphandle(struct device_node *np, const char *propname,
  350. int index)
  351. {
  352. struct of_phandle_args args;
  353. struct framer *framer;
  354. int ret;
  355. ret = of_parse_phandle_with_optional_args(np, propname, "#framer-cells", index, &args);
  356. if (ret)
  357. return ERR_PTR(-ENODEV);
  358. if (!of_device_is_available(args.np)) {
  359. framer = ERR_PTR(-ENODEV);
  360. goto out_node_put;
  361. }
  362. framer = framer_of_get_from_provider(&args);
  363. out_node_put:
  364. of_node_put(args.np);
  365. return framer;
  366. }
  367. static struct framer *framer_of_get_byparent(struct device_node *np, int index)
  368. {
  369. struct of_phandle_args args;
  370. struct framer *framer;
  371. args.np = of_get_parent(np);
  372. args.args_count = 1;
  373. args.args[0] = index;
  374. while (args.np) {
  375. framer = framer_of_get_from_provider(&args);
  376. if (IS_ERR(framer) && PTR_ERR(framer) != -EPROBE_DEFER) {
  377. args.np = of_get_next_parent(args.np);
  378. continue;
  379. }
  380. of_node_put(args.np);
  381. return framer;
  382. }
  383. return ERR_PTR(-ENODEV);
  384. }
  385. /**
  386. * framer_get() - lookup and obtain a reference to a framer.
  387. * @dev: device that requests the framer
  388. * @con_id: name of the framer from device's point of view
  389. *
  390. * Returns the framer driver, after getting a refcount to it; or
  391. * -ENODEV if there is no such framer. The caller is responsible for
  392. * calling framer_put() to release that count.
  393. */
  394. struct framer *framer_get(struct device *dev, const char *con_id)
  395. {
  396. struct framer *framer = ERR_PTR(-ENODEV);
  397. struct device_link *link;
  398. int ret;
  399. if (dev->of_node) {
  400. if (con_id)
  401. framer = framer_of_get_byphandle(dev->of_node, con_id, 0);
  402. else
  403. framer = framer_of_get_byparent(dev->of_node, 0);
  404. }
  405. if (IS_ERR(framer))
  406. return framer;
  407. get_device(&framer->dev);
  408. if (!try_module_get(framer->ops->owner)) {
  409. ret = -EPROBE_DEFER;
  410. goto err_put_device;
  411. }
  412. link = device_link_add(dev, &framer->dev, DL_FLAG_STATELESS);
  413. if (!link) {
  414. dev_err(dev, "failed to create device_link to %s\n", dev_name(&framer->dev));
  415. ret = -EPROBE_DEFER;
  416. goto err_module_put;
  417. }
  418. return framer;
  419. err_module_put:
  420. module_put(framer->ops->owner);
  421. err_put_device:
  422. put_device(&framer->dev);
  423. return ERR_PTR(ret);
  424. }
  425. EXPORT_SYMBOL_GPL(framer_get);
  426. /**
  427. * framer_put() - release the framer
  428. * @dev: device that wants to release this framer
  429. * @framer: the framer returned by framer_get()
  430. *
  431. * Releases a refcount the caller received from framer_get().
  432. */
  433. void framer_put(struct device *dev, struct framer *framer)
  434. {
  435. device_link_remove(dev, &framer->dev);
  436. module_put(framer->ops->owner);
  437. put_device(&framer->dev);
  438. }
  439. EXPORT_SYMBOL_GPL(framer_put);
  440. static void devm_framer_put(struct device *dev, void *res)
  441. {
  442. struct framer *framer = *(struct framer **)res;
  443. framer_put(dev, framer);
  444. }
  445. /**
  446. * devm_framer_get() - lookup and obtain a reference to a framer.
  447. * @dev: device that requests this framer
  448. * @con_id: name of the framer from device's point of view
  449. *
  450. * Gets the framer using framer_get(), and associates a device with it using
  451. * devres. On driver detach, framer_put() function is invoked on the devres
  452. * data, then, devres data is freed.
  453. */
  454. struct framer *devm_framer_get(struct device *dev, const char *con_id)
  455. {
  456. struct framer **ptr, *framer;
  457. ptr = devres_alloc(devm_framer_put, sizeof(*ptr), GFP_KERNEL);
  458. if (!ptr)
  459. return ERR_PTR(-ENOMEM);
  460. framer = framer_get(dev, con_id);
  461. if (!IS_ERR(framer)) {
  462. *ptr = framer;
  463. devres_add(dev, ptr);
  464. } else {
  465. devres_free(ptr);
  466. return framer;
  467. }
  468. return framer;
  469. }
  470. EXPORT_SYMBOL_GPL(devm_framer_get);
  471. /**
  472. * devm_framer_optional_get() - lookup and obtain a reference to an optional
  473. * framer.
  474. * @dev: device that requests this framer
  475. * @con_id: name of the framer from device's point of view
  476. *
  477. * Same as devm_framer_get() except that if the framer does not exist, it is not
  478. * considered an error and -ENODEV will not be returned. Instead the NULL framer
  479. * is returned.
  480. */
  481. struct framer *devm_framer_optional_get(struct device *dev, const char *con_id)
  482. {
  483. struct framer *framer = devm_framer_get(dev, con_id);
  484. if (PTR_ERR(framer) == -ENODEV)
  485. framer = NULL;
  486. return framer;
  487. }
  488. EXPORT_SYMBOL_GPL(devm_framer_optional_get);
  489. static void framer_notify_status_work(struct work_struct *work)
  490. {
  491. struct framer *framer = container_of(work, struct framer, notify_status_work);
  492. blocking_notifier_call_chain(&framer->notifier_list, FRAMER_EVENT_STATUS, NULL);
  493. }
  494. void framer_notify_status_change(struct framer *framer)
  495. {
  496. /* Can be called from atomic context -> just schedule a task to call
  497. * blocking notifiers
  498. */
  499. queue_work(system_power_efficient_wq, &framer->notify_status_work);
  500. }
  501. EXPORT_SYMBOL_GPL(framer_notify_status_change);
  502. /**
  503. * framer_create() - create a new framer
  504. * @dev: device that is creating the new framer
  505. * @node: device node of the framer. default to dev->of_node.
  506. * @ops: function pointers for performing framer operations
  507. *
  508. * Called to create a framer using framer framework.
  509. */
  510. struct framer *framer_create(struct device *dev, struct device_node *node,
  511. const struct framer_ops *ops)
  512. {
  513. struct framer *framer;
  514. int ret;
  515. int id;
  516. /* get_status() is mandatory if the provider ask for polling status */
  517. if (WARN_ON((ops->flags & FRAMER_FLAG_POLL_STATUS) && !ops->get_status))
  518. return ERR_PTR(-EINVAL);
  519. framer = kzalloc_obj(*framer);
  520. if (!framer)
  521. return ERR_PTR(-ENOMEM);
  522. id = ida_alloc(&framer_ida, GFP_KERNEL);
  523. if (id < 0) {
  524. dev_err(dev, "unable to get id\n");
  525. ret = id;
  526. goto free_framer;
  527. }
  528. device_initialize(&framer->dev);
  529. mutex_init(&framer->mutex);
  530. INIT_WORK(&framer->notify_status_work, framer_notify_status_work);
  531. INIT_DELAYED_WORK(&framer->polling_work, framer_polling_work);
  532. BLOCKING_INIT_NOTIFIER_HEAD(&framer->notifier_list);
  533. framer->dev.class = &framer_class;
  534. framer->dev.parent = dev;
  535. framer->dev.of_node = node ? node : dev->of_node;
  536. framer->id = id;
  537. framer->ops = ops;
  538. ret = dev_set_name(&framer->dev, "framer-%s.%d", dev_name(dev), id);
  539. if (ret)
  540. goto put_dev;
  541. /* framer-supply */
  542. framer->pwr = regulator_get_optional(&framer->dev, "framer");
  543. if (IS_ERR(framer->pwr)) {
  544. ret = PTR_ERR(framer->pwr);
  545. if (ret == -EPROBE_DEFER)
  546. goto put_dev;
  547. framer->pwr = NULL;
  548. }
  549. ret = device_add(&framer->dev);
  550. if (ret)
  551. goto put_dev;
  552. if (pm_runtime_enabled(dev)) {
  553. pm_runtime_enable(&framer->dev);
  554. pm_runtime_no_callbacks(&framer->dev);
  555. }
  556. return framer;
  557. put_dev:
  558. put_device(&framer->dev); /* calls framer_release() which frees resources */
  559. return ERR_PTR(ret);
  560. free_framer:
  561. kfree(framer);
  562. return ERR_PTR(ret);
  563. }
  564. EXPORT_SYMBOL_GPL(framer_create);
  565. /**
  566. * framer_destroy() - destroy the framer
  567. * @framer: the framer to be destroyed
  568. *
  569. * Called to destroy the framer.
  570. */
  571. void framer_destroy(struct framer *framer)
  572. {
  573. /* polling_work should already be stopped but if framer_exit() was not
  574. * called (bug), here it's the last time to do that ...
  575. */
  576. cancel_delayed_work_sync(&framer->polling_work);
  577. cancel_work_sync(&framer->notify_status_work);
  578. pm_runtime_disable(&framer->dev);
  579. device_unregister(&framer->dev); /* calls framer_release() which frees resources */
  580. }
  581. EXPORT_SYMBOL_GPL(framer_destroy);
  582. static void devm_framer_destroy(struct device *dev, void *res)
  583. {
  584. struct framer *framer = *(struct framer **)res;
  585. framer_destroy(framer);
  586. }
  587. /**
  588. * devm_framer_create() - create a new framer
  589. * @dev: device that is creating the new framer
  590. * @node: device node of the framer
  591. * @ops: function pointers for performing framer operations
  592. *
  593. * Creates a new framer device adding it to the framer class.
  594. * While at that, it also associates the device with the framer using devres.
  595. * On driver detach, release function is invoked on the devres data,
  596. * then, devres data is freed.
  597. */
  598. struct framer *devm_framer_create(struct device *dev, struct device_node *node,
  599. const struct framer_ops *ops)
  600. {
  601. struct framer **ptr, *framer;
  602. ptr = devres_alloc(devm_framer_destroy, sizeof(*ptr), GFP_KERNEL);
  603. if (!ptr)
  604. return ERR_PTR(-ENOMEM);
  605. framer = framer_create(dev, node, ops);
  606. if (!IS_ERR(framer)) {
  607. *ptr = framer;
  608. devres_add(dev, ptr);
  609. } else {
  610. devres_free(ptr);
  611. }
  612. return framer;
  613. }
  614. EXPORT_SYMBOL_GPL(devm_framer_create);
  615. /**
  616. * framer_provider_simple_of_xlate() - returns the framer instance from framer provider
  617. * @dev: the framer provider device (not used here)
  618. * @args: of_phandle_args
  619. *
  620. * Intended to be used by framer provider for the common case where #framer-cells is
  621. * 0. For other cases where #framer-cells is greater than '0', the framer provider
  622. * should provide a custom of_xlate function that reads the *args* and returns
  623. * the appropriate framer.
  624. */
  625. struct framer *framer_provider_simple_of_xlate(struct device *dev,
  626. const struct of_phandle_args *args)
  627. {
  628. struct device *target_dev;
  629. target_dev = class_find_device_by_of_node(&framer_class, args->np);
  630. if (!target_dev)
  631. return ERR_PTR(-ENODEV);
  632. put_device(target_dev);
  633. return dev_to_framer(target_dev);
  634. }
  635. EXPORT_SYMBOL_GPL(framer_provider_simple_of_xlate);
  636. /**
  637. * __framer_provider_of_register() - create/register framer provider with the framework
  638. * @dev: struct device of the framer provider
  639. * @owner: the module owner containing of_xlate
  640. * @of_xlate: function pointer to obtain framer instance from framer provider
  641. *
  642. * Creates struct framer_provider from dev and of_xlate function pointer.
  643. * This is used in the case of dt boot for finding the framer instance from
  644. * framer provider.
  645. */
  646. struct framer_provider *
  647. __framer_provider_of_register(struct device *dev, struct module *owner,
  648. struct framer *(*of_xlate)(struct device *dev,
  649. const struct of_phandle_args *args))
  650. {
  651. struct framer_provider *framer_provider;
  652. framer_provider = kzalloc_obj(*framer_provider);
  653. if (!framer_provider)
  654. return ERR_PTR(-ENOMEM);
  655. framer_provider->dev = dev;
  656. framer_provider->owner = owner;
  657. framer_provider->of_xlate = of_xlate;
  658. of_node_get(framer_provider->dev->of_node);
  659. mutex_lock(&framer_provider_mutex);
  660. list_add_tail(&framer_provider->list, &framer_provider_list);
  661. mutex_unlock(&framer_provider_mutex);
  662. return framer_provider;
  663. }
  664. EXPORT_SYMBOL_GPL(__framer_provider_of_register);
  665. /**
  666. * framer_provider_of_unregister() - unregister framer provider from the framework
  667. * @framer_provider: framer provider returned by framer_provider_of_register()
  668. *
  669. * Removes the framer_provider created using framer_provider_of_register().
  670. */
  671. void framer_provider_of_unregister(struct framer_provider *framer_provider)
  672. {
  673. mutex_lock(&framer_provider_mutex);
  674. list_del(&framer_provider->list);
  675. mutex_unlock(&framer_provider_mutex);
  676. of_node_put(framer_provider->dev->of_node);
  677. kfree(framer_provider);
  678. }
  679. EXPORT_SYMBOL_GPL(framer_provider_of_unregister);
  680. static void devm_framer_provider_of_unregister(struct device *dev, void *res)
  681. {
  682. struct framer_provider *framer_provider = *(struct framer_provider **)res;
  683. framer_provider_of_unregister(framer_provider);
  684. }
  685. /**
  686. * __devm_framer_provider_of_register() - create/register framer provider with
  687. * the framework
  688. * @dev: struct device of the framer provider
  689. * @owner: the module owner containing of_xlate
  690. * @of_xlate: function pointer to obtain framer instance from framer provider
  691. *
  692. * Creates struct framer_provider from dev and of_xlate function pointer.
  693. * This is used in the case of dt boot for finding the framer instance from
  694. * framer provider. While at that, it also associates the device with the
  695. * framer provider using devres. On driver detach, release function is invoked
  696. * on the devres data, then, devres data is freed.
  697. */
  698. struct framer_provider *
  699. __devm_framer_provider_of_register(struct device *dev, struct module *owner,
  700. struct framer *(*of_xlate)(struct device *dev,
  701. const struct of_phandle_args *args))
  702. {
  703. struct framer_provider **ptr, *framer_provider;
  704. ptr = devres_alloc(devm_framer_provider_of_unregister, sizeof(*ptr), GFP_KERNEL);
  705. if (!ptr)
  706. return ERR_PTR(-ENOMEM);
  707. framer_provider = __framer_provider_of_register(dev, owner, of_xlate);
  708. if (!IS_ERR(framer_provider)) {
  709. *ptr = framer_provider;
  710. devres_add(dev, ptr);
  711. } else {
  712. devres_free(ptr);
  713. }
  714. return framer_provider;
  715. }
  716. EXPORT_SYMBOL_GPL(__devm_framer_provider_of_register);
  717. /**
  718. * framer_release() - release the framer
  719. * @dev: the dev member within framer
  720. *
  721. * When the last reference to the device is removed, it is called
  722. * from the embedded kobject as release method.
  723. */
  724. static void framer_release(struct device *dev)
  725. {
  726. struct framer *framer;
  727. framer = dev_to_framer(dev);
  728. regulator_put(framer->pwr);
  729. ida_free(&framer_ida, framer->id);
  730. kfree(framer);
  731. }
  732. static int __init framer_core_init(void)
  733. {
  734. return class_register(&framer_class);
  735. }
  736. device_initcall(framer_core_init);