spmi.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. /* Copyright (c) 2012-2014, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #define pr_fmt(fmt) "%s: " fmt, __func__
  13. #include <linux/kernel.h>
  14. #include <linux/errno.h>
  15. #include <linux/idr.h>
  16. #include <linux/slab.h>
  17. #include <linux/of_device.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/spmi.h>
  20. #include <linux/module.h>
  21. #include <linux/pm_runtime.h>
  22. #include "spmi-dbgfs.h"
  23. struct spmii_boardinfo {
  24. struct list_head list;
  25. struct spmi_boardinfo board_info;
  26. };
  27. static DEFINE_MUTEX(board_lock);
  28. static LIST_HEAD(board_list);
  29. static DEFINE_IDR(ctrl_idr);
  30. static DEFINE_IDA(spmi_devid_ida);
  31. static struct device_type spmi_dev_type;
  32. static struct device_type spmi_ctrl_type;
  33. #if (!defined(CONFIG_MACH_CT01) && !defined(CONFIG_MACH_CT01_CHN_CU))
  34. /* for global use */
  35. struct spmi_controller *spmi_ctrl_extra;
  36. #endif
  37. /* Forward declarations */
  38. struct bus_type spmi_bus_type;
  39. static int spmi_register_controller(struct spmi_controller *ctrl);
  40. /**
  41. * spmi_busnum_to_ctrl: Map bus number to controller
  42. * @busnum: bus number
  43. * Returns controller representing this bus number
  44. */
  45. struct spmi_controller *spmi_busnum_to_ctrl(u32 bus_num)
  46. {
  47. struct spmi_controller *ctrl;
  48. mutex_lock(&board_lock);
  49. ctrl = idr_find(&ctrl_idr, bus_num);
  50. mutex_unlock(&board_lock);
  51. return ctrl;
  52. }
  53. EXPORT_SYMBOL_GPL(spmi_busnum_to_ctrl);
  54. /**
  55. * spmi_add_controller: Controller bring-up.
  56. * @ctrl: controller to be registered.
  57. * A controller is registered with the framework using this API. ctrl->nr is the
  58. * desired number with which SPMI framework registers the controller.
  59. * Function will return -EBUSY if the number is in use.
  60. */
  61. int spmi_add_controller(struct spmi_controller *ctrl)
  62. {
  63. int id;
  64. int status;
  65. if (!ctrl)
  66. return -EINVAL;
  67. pr_debug("adding controller for bus %d (0x%p)\n", ctrl->nr, ctrl);
  68. if (ctrl->nr & ~MAX_ID_MASK) {
  69. pr_err("invalid bus identifier %d\n", ctrl->nr);
  70. return -EINVAL;
  71. }
  72. retry:
  73. if (idr_pre_get(&ctrl_idr, GFP_KERNEL) == 0) {
  74. pr_err("no free memory for idr\n");
  75. return -ENOMEM;
  76. }
  77. mutex_lock(&board_lock);
  78. status = idr_get_new_above(&ctrl_idr, ctrl, ctrl->nr, &id);
  79. if (status == 0 && id != ctrl->nr) {
  80. status = -EBUSY;
  81. idr_remove(&ctrl_idr, id);
  82. }
  83. mutex_unlock(&board_lock);
  84. if (status == -EAGAIN)
  85. goto retry;
  86. if (status == 0)
  87. status = spmi_register_controller(ctrl);
  88. return status;
  89. }
  90. EXPORT_SYMBOL_GPL(spmi_add_controller);
  91. /* Remove a device associated with a controller */
  92. static int spmi_ctrl_remove_device(struct device *dev, void *data)
  93. {
  94. struct spmi_device *spmidev = to_spmi_device(dev);
  95. struct spmi_controller *ctrl = data;
  96. if (dev->type == &spmi_dev_type && spmidev->ctrl == ctrl)
  97. spmi_remove_device(spmidev);
  98. return 0;
  99. }
  100. /**
  101. * spmi_del_controller: Controller tear-down.
  102. * @ctrl: controller to be removed.
  103. *
  104. * Controller added with the above API is torn down using this API.
  105. */
  106. int spmi_del_controller(struct spmi_controller *ctrl)
  107. {
  108. struct spmi_controller *found;
  109. if (!ctrl)
  110. return -EINVAL;
  111. /* Check that the ctrl has been added */
  112. mutex_lock(&board_lock);
  113. found = idr_find(&ctrl_idr, ctrl->nr);
  114. mutex_unlock(&board_lock);
  115. if (found != ctrl)
  116. return -EINVAL;
  117. /* Remove all the clients associated with this controller */
  118. mutex_lock(&board_lock);
  119. bus_for_each_dev(&spmi_bus_type, NULL, ctrl, spmi_ctrl_remove_device);
  120. mutex_unlock(&board_lock);
  121. spmi_dfs_del_controller(ctrl);
  122. mutex_lock(&board_lock);
  123. idr_remove(&ctrl_idr, ctrl->nr);
  124. mutex_unlock(&board_lock);
  125. init_completion(&ctrl->dev_released);
  126. device_unregister(&ctrl->dev);
  127. wait_for_completion(&ctrl->dev_released);
  128. return 0;
  129. }
  130. EXPORT_SYMBOL_GPL(spmi_del_controller);
  131. #define spmi_ctrl_attr_gr NULL
  132. static void spmi_ctrl_release(struct device *dev)
  133. {
  134. struct spmi_controller *ctrl = to_spmi_controller(dev);
  135. complete(&ctrl->dev_released);
  136. }
  137. static struct device_type spmi_ctrl_type = {
  138. .groups = spmi_ctrl_attr_gr,
  139. .release = spmi_ctrl_release,
  140. };
  141. #define spmi_device_attr_gr NULL
  142. #define spmi_device_uevent NULL
  143. static void spmi_dev_release(struct device *dev)
  144. {
  145. struct spmi_device *spmidev = to_spmi_device(dev);
  146. kfree(spmidev);
  147. }
  148. static struct device_type spmi_dev_type = {
  149. .groups = spmi_device_attr_gr,
  150. .uevent = spmi_device_uevent,
  151. .release = spmi_dev_release,
  152. };
  153. /**
  154. * spmi_alloc_device: Allocate a new SPMI devices.
  155. * @ctrl: controller to which this device is to be added to.
  156. * Context: can sleep
  157. *
  158. * Allows a driver to allocate and initialize a SPMI device without
  159. * registering it immediately. This allows a driver to directly fill
  160. * the spmi_device structure before calling spmi_add_device().
  161. *
  162. * Caller is responsible to call spmi_add_device() on the returned
  163. * spmi_device. If the caller needs to discard the spmi_device without
  164. * adding it, then spmi_dev_put() should be called.
  165. */
  166. struct spmi_device *spmi_alloc_device(struct spmi_controller *ctrl)
  167. {
  168. struct spmi_device *spmidev;
  169. if (!ctrl || !spmi_busnum_to_ctrl(ctrl->nr)) {
  170. pr_err("Missing SPMI controller\n");
  171. return NULL;
  172. }
  173. spmidev = kzalloc(sizeof(*spmidev), GFP_KERNEL);
  174. if (!spmidev) {
  175. dev_err(&ctrl->dev, "unable to allocate spmi_device\n");
  176. return NULL;
  177. }
  178. spmidev->ctrl = ctrl;
  179. spmidev->dev.parent = ctrl->dev.parent;
  180. spmidev->dev.bus = &spmi_bus_type;
  181. spmidev->dev.type = &spmi_dev_type;
  182. device_initialize(&spmidev->dev);
  183. return spmidev;
  184. }
  185. EXPORT_SYMBOL_GPL(spmi_alloc_device);
  186. /* Validate the SPMI device structure */
  187. static struct device *get_valid_device(struct spmi_device *spmidev)
  188. {
  189. struct device *dev;
  190. if (!spmidev)
  191. return NULL;
  192. dev = &spmidev->dev;
  193. if (dev->bus != &spmi_bus_type || dev->type != &spmi_dev_type)
  194. return NULL;
  195. if (!spmidev->ctrl || !spmi_busnum_to_ctrl(spmidev->ctrl->nr))
  196. return NULL;
  197. return dev;
  198. }
  199. /**
  200. * spmi_add_device: Add a new device without register board info.
  201. * @spmi_dev: spmi_device to be added (registered).
  202. *
  203. * Called when device doesn't have an explicit client-driver to be probed, or
  204. * the client-driver is a module installed dynamically.
  205. */
  206. int spmi_add_device(struct spmi_device *spmidev)
  207. {
  208. int rc;
  209. struct device *dev = get_valid_device(spmidev);
  210. int id;
  211. if (!dev) {
  212. pr_err("invalid SPMI device\n");
  213. return -EINVAL;
  214. }
  215. id = ida_simple_get(&spmi_devid_ida, 0, 0, GFP_KERNEL);
  216. if (id < 0) {
  217. pr_err("No id available status = %d\n", id);
  218. return id;
  219. }
  220. /* Set the device name */
  221. spmidev->id = id;
  222. dev_set_name(dev, "%s-%d", spmidev->name, spmidev->id);
  223. /* Device may be bound to an active driver when this returns */
  224. rc = device_add(dev);
  225. if (rc < 0) {
  226. ida_simple_remove(&spmi_devid_ida, spmidev->id);
  227. dev_err(dev, "Can't add %s, status %d\n", dev_name(dev), rc);
  228. } else {
  229. dev_dbg(dev, "device %s registered\n", dev_name(dev));
  230. }
  231. return rc;
  232. }
  233. EXPORT_SYMBOL_GPL(spmi_add_device);
  234. /**
  235. * spmi_new_device: Instantiates a new SPMI device
  236. * @ctrl: controller to which this device is to be added to.
  237. * @info: board information for this device.
  238. *
  239. * Returns the new device or NULL.
  240. */
  241. struct spmi_device *spmi_new_device(struct spmi_controller *ctrl,
  242. struct spmi_boardinfo const *info)
  243. {
  244. struct spmi_device *spmidev;
  245. int rc;
  246. if (!ctrl || !info)
  247. return NULL;
  248. spmidev = spmi_alloc_device(ctrl);
  249. if (!spmidev)
  250. return NULL;
  251. spmidev->name = info->name;
  252. spmidev->sid = info->slave_id;
  253. spmidev->dev.of_node = info->of_node;
  254. spmidev->dev.platform_data = (void *)info->platform_data;
  255. spmidev->num_dev_node = info->num_dev_node;
  256. spmidev->dev_node = info->dev_node;
  257. spmidev->res = info->res;
  258. rc = spmi_add_device(spmidev);
  259. if (rc < 0) {
  260. spmi_dev_put(spmidev);
  261. return NULL;
  262. }
  263. return spmidev;
  264. }
  265. EXPORT_SYMBOL_GPL(spmi_new_device);
  266. /* spmi_remove_device: Remove the effect of spmi_add_device() */
  267. void spmi_remove_device(struct spmi_device *spmi_dev)
  268. {
  269. device_unregister(&spmi_dev->dev);
  270. ida_simple_remove(&spmi_devid_ida, spmi_dev->id);
  271. }
  272. EXPORT_SYMBOL_GPL(spmi_remove_device);
  273. static void spmi_match_ctrl_to_boardinfo(struct spmi_controller *ctrl,
  274. struct spmi_boardinfo *bi)
  275. {
  276. struct spmi_device *spmidev;
  277. spmidev = spmi_new_device(ctrl, bi);
  278. if (!spmidev)
  279. dev_err(ctrl->dev.parent, "can't create new device for %s\n",
  280. bi->name);
  281. }
  282. /**
  283. * spmi_register_board_info: Board-initialization routine.
  284. * @bus_num: controller number (bus) on which this device will sit.
  285. * @info: list of all devices on all controllers present on the board.
  286. * @n: number of entries.
  287. * API enumerates respective devices on corresponding controller.
  288. * Called from board-init function.
  289. * If controller is not present, only add to boards list
  290. */
  291. int spmi_register_board_info(int busnum,
  292. struct spmi_boardinfo const *info, unsigned n)
  293. {
  294. int i;
  295. struct spmii_boardinfo *bi;
  296. struct spmi_controller *ctrl;
  297. bi = kzalloc(n * sizeof(*bi), GFP_KERNEL);
  298. if (!bi)
  299. return -ENOMEM;
  300. ctrl = spmi_busnum_to_ctrl(busnum);
  301. for (i = 0; i < n; i++, bi++, info++) {
  302. memcpy(&bi->board_info, info, sizeof(*info));
  303. mutex_lock(&board_lock);
  304. list_add_tail(&bi->list, &board_list);
  305. if (ctrl)
  306. spmi_match_ctrl_to_boardinfo(ctrl, &bi->board_info);
  307. mutex_unlock(&board_lock);
  308. }
  309. return 0;
  310. }
  311. EXPORT_SYMBOL_GPL(spmi_register_board_info);
  312. /* ------------------------------------------------------------------------- */
  313. static inline int
  314. spmi_cmd(struct spmi_controller *ctrl, u8 opcode, u8 sid)
  315. {
  316. if (!ctrl || !ctrl->cmd || ctrl->dev.type != &spmi_ctrl_type)
  317. return -EINVAL;
  318. return ctrl->cmd(ctrl, opcode, sid);
  319. }
  320. static inline int spmi_read_cmd(struct spmi_controller *ctrl,
  321. u8 opcode, u8 sid, u16 addr, u8 bc, u8 *buf)
  322. {
  323. if (!ctrl || !ctrl->read_cmd || ctrl->dev.type != &spmi_ctrl_type)
  324. return -EINVAL;
  325. return ctrl->read_cmd(ctrl, opcode, sid, addr, bc, buf);
  326. }
  327. static inline int spmi_write_cmd(struct spmi_controller *ctrl,
  328. u8 opcode, u8 sid, u16 addr, u8 bc, u8 *buf)
  329. {
  330. if (!ctrl || !ctrl->write_cmd || ctrl->dev.type != &spmi_ctrl_type)
  331. return -EINVAL;
  332. return ctrl->write_cmd(ctrl, opcode, sid, addr, bc, buf);
  333. }
  334. /*
  335. * register read/write: 5-bit address, 1 byte of data
  336. * extended register read/write: 8-bit address, up to 16 bytes of data
  337. * extended register read/write long: 16-bit address, up to 8 bytes of data
  338. */
  339. /**
  340. * spmi_register_read() - register read
  341. * @dev: SPMI device.
  342. * @sid: slave identifier.
  343. * @ad: slave register address (5-bit address).
  344. * @buf: buffer to be populated with data from the Slave.
  345. *
  346. * Reads 1 byte of data from a Slave device register.
  347. */
  348. int spmi_register_read(struct spmi_controller *ctrl, u8 sid, u8 addr, u8 *buf)
  349. {
  350. /* 4-bit Slave Identifier, 5-bit register address */
  351. if (sid > SPMI_MAX_SLAVE_ID || addr > 0x1F)
  352. return -EINVAL;
  353. return spmi_read_cmd(ctrl, SPMI_CMD_READ, sid, addr, 0, buf);
  354. }
  355. EXPORT_SYMBOL_GPL(spmi_register_read);
  356. /**
  357. * spmi_ext_register_read() - extended register read
  358. * @dev: SPMI device.
  359. * @sid: slave identifier.
  360. * @ad: slave register address (8-bit address).
  361. * @len: the request number of bytes to read (up to 16 bytes).
  362. * @buf: buffer to be populated with data from the Slave.
  363. *
  364. * Reads up to 16 bytes of data from the extended register space on a
  365. * Slave device.
  366. */
  367. int spmi_ext_register_read(struct spmi_controller *ctrl,
  368. u8 sid, u8 addr, u8 *buf, int len)
  369. {
  370. /* 4-bit Slave Identifier, 8-bit register address, up to 16 bytes */
  371. if (sid > SPMI_MAX_SLAVE_ID || len <= 0 || len > 16)
  372. return -EINVAL;
  373. return spmi_read_cmd(ctrl, SPMI_CMD_EXT_READ, sid, addr, len - 1, buf);
  374. }
  375. EXPORT_SYMBOL_GPL(spmi_ext_register_read);
  376. /**
  377. * spmi_ext_register_readl() - extended register read long
  378. * @dev: SPMI device.
  379. * @sid: slave identifier.
  380. * @ad: slave register address (16-bit address).
  381. * @len: the request number of bytes to read (up to 8 bytes).
  382. * @buf: buffer to be populated with data from the Slave.
  383. *
  384. * Reads up to 8 bytes of data from the extended register space on a
  385. * Slave device using 16-bit address.
  386. */
  387. int spmi_ext_register_readl(struct spmi_controller *ctrl,
  388. u8 sid, u16 addr, u8 *buf, int len)
  389. {
  390. /* 4-bit Slave Identifier, 16-bit register address, up to 8 bytes */
  391. if (sid > SPMI_MAX_SLAVE_ID || len <= 0 || len > 8)
  392. return -EINVAL;
  393. return spmi_read_cmd(ctrl, SPMI_CMD_EXT_READL, sid, addr, len - 1, buf);
  394. }
  395. EXPORT_SYMBOL_GPL(spmi_ext_register_readl);
  396. #if (!defined(CONFIG_MACH_CT01) && !defined(CONFIG_MACH_CT01_CHN_CU))
  397. int spmi_ext_register_readl_extra(u8 sid, u16 addr, u8 *buf, int len)
  398. {
  399. /* 4-bit Slave Identifier, 16-bit register address, up to 8 bytes */
  400. if (sid > SPMI_MAX_SLAVE_ID || len <= 0 || len > 8)
  401. return -EINVAL;
  402. return spmi_read_cmd(spmi_ctrl_extra, SPMI_CMD_EXT_READL, sid, addr, len - 1, buf);
  403. }
  404. EXPORT_SYMBOL_GPL(spmi_ext_register_readl_extra);
  405. #endif
  406. /**
  407. * spmi_register_write() - register write
  408. * @dev: SPMI device.
  409. * @sid: slave identifier.
  410. * @ad: slave register address (5-bit address).
  411. * @buf: buffer containing the data to be transferred to the Slave.
  412. *
  413. * Writes 1 byte of data to a Slave device register.
  414. */
  415. int spmi_register_write(struct spmi_controller *ctrl, u8 sid, u8 addr, u8 *buf)
  416. {
  417. u8 op = SPMI_CMD_WRITE;
  418. /* 4-bit Slave Identifier, 5-bit register address */
  419. if (sid > SPMI_MAX_SLAVE_ID || addr > 0x1F)
  420. return -EINVAL;
  421. return spmi_write_cmd(ctrl, op, sid, addr, 0, buf);
  422. }
  423. EXPORT_SYMBOL_GPL(spmi_register_write);
  424. /**
  425. * spmi_register_zero_write() - register zero write
  426. * @dev: SPMI device.
  427. * @sid: slave identifier.
  428. * @data: the data to be written to register 0 (7-bits).
  429. *
  430. * Writes data to register 0 of the Slave device.
  431. */
  432. int spmi_register_zero_write(struct spmi_controller *ctrl, u8 sid, u8 data)
  433. {
  434. u8 op = SPMI_CMD_ZERO_WRITE;
  435. /* 4-bit Slave Identifier, 5-bit register address */
  436. if (sid > SPMI_MAX_SLAVE_ID)
  437. return -EINVAL;
  438. return spmi_write_cmd(ctrl, op, sid, 0, 0, &data);
  439. }
  440. EXPORT_SYMBOL_GPL(spmi_register_zero_write);
  441. /**
  442. * spmi_ext_register_write() - extended register write
  443. * @dev: SPMI device.
  444. * @sid: slave identifier.
  445. * @ad: slave register address (8-bit address).
  446. * @buf: buffer containing the data to be transferred to the Slave.
  447. * @len: the request number of bytes to read (up to 16 bytes).
  448. *
  449. * Writes up to 16 bytes of data to the extended register space of a
  450. * Slave device.
  451. */
  452. int spmi_ext_register_write(struct spmi_controller *ctrl,
  453. u8 sid, u8 addr, u8 *buf, int len)
  454. {
  455. u8 op = SPMI_CMD_EXT_WRITE;
  456. /* 4-bit Slave Identifier, 8-bit register address, up to 16 bytes */
  457. if (sid > SPMI_MAX_SLAVE_ID || len <= 0 || len > 16)
  458. return -EINVAL;
  459. return spmi_write_cmd(ctrl, op, sid, addr, len - 1, buf);
  460. }
  461. EXPORT_SYMBOL_GPL(spmi_ext_register_write);
  462. /**
  463. * spmi_ext_register_writel() - extended register write long
  464. * @dev: SPMI device.
  465. * @sid: slave identifier.
  466. * @ad: slave register address (16-bit address).
  467. * @buf: buffer containing the data to be transferred to the Slave.
  468. * @len: the request number of bytes to read (up to 8 bytes).
  469. *
  470. * Writes up to 8 bytes of data to the extended register space of a
  471. * Slave device using 16-bit address.
  472. */
  473. int spmi_ext_register_writel(struct spmi_controller *ctrl,
  474. u8 sid, u16 addr, u8 *buf, int len)
  475. {
  476. u8 op = SPMI_CMD_EXT_WRITEL;
  477. /* 4-bit Slave Identifier, 16-bit register address, up to 8 bytes */
  478. if (sid > SPMI_MAX_SLAVE_ID || len <= 0 || len > 8)
  479. return -EINVAL;
  480. return spmi_write_cmd(ctrl, op, sid, addr, len - 1, buf);
  481. }
  482. EXPORT_SYMBOL_GPL(spmi_ext_register_writel);
  483. #if (!defined(CONFIG_MACH_CT01) && !defined(CONFIG_MACH_CT01_CHN_CU))
  484. int spmi_ext_register_writel_extra(u8 sid, u16 addr, u8 *buf, int len)
  485. {
  486. u8 op = SPMI_CMD_EXT_WRITEL;
  487. /* 4-bit Slave Identifier, 16-bit register address, up to 8 bytes */
  488. if (sid > SPMI_MAX_SLAVE_ID || len <= 0 || len > 8)
  489. return -EINVAL;
  490. return spmi_write_cmd(spmi_ctrl_extra, op, sid, addr, len - 1, buf);
  491. }
  492. EXPORT_SYMBOL_GPL(spmi_ext_register_writel_extra);
  493. #endif
  494. /**
  495. * spmi_command_reset() - sends RESET command to the specified slave
  496. * @dev: SPMI device.
  497. * @sid: slave identifier.
  498. *
  499. * The Reset command initializes the Slave and forces all registers to
  500. * their reset values. The Slave shall enter the STARTUP state after
  501. * receiving a Reset command.
  502. *
  503. * Returns
  504. * -EINVAL for invalid Slave Identifier.
  505. * -EPERM if the SPMI transaction is denied due to permission issues.
  506. * -EIO if the SPMI transaction fails (parity errors, etc).
  507. * -ETIMEDOUT if the SPMI transaction times out.
  508. */
  509. int spmi_command_reset(struct spmi_controller *ctrl, u8 sid)
  510. {
  511. if (sid > SPMI_MAX_SLAVE_ID)
  512. return -EINVAL;
  513. return spmi_cmd(ctrl, SPMI_CMD_RESET, sid);
  514. }
  515. EXPORT_SYMBOL_GPL(spmi_command_reset);
  516. /**
  517. * spmi_command_sleep() - sends SLEEP command to the specified slave
  518. * @dev: SPMI device.
  519. * @sid: slave identifier.
  520. *
  521. * The Sleep command causes the Slave to enter the user defined SLEEP state.
  522. *
  523. * Returns
  524. * -EINVAL for invalid Slave Identifier.
  525. * -EPERM if the SPMI transaction is denied due to permission issues.
  526. * -EIO if the SPMI transaction fails (parity errors, etc).
  527. * -ETIMEDOUT if the SPMI transaction times out.
  528. */
  529. int spmi_command_sleep(struct spmi_controller *ctrl, u8 sid)
  530. {
  531. if (sid > SPMI_MAX_SLAVE_ID)
  532. return -EINVAL;
  533. return spmi_cmd(ctrl, SPMI_CMD_SLEEP, sid);
  534. }
  535. EXPORT_SYMBOL_GPL(spmi_command_sleep);
  536. /**
  537. * spmi_command_wakeup() - sends WAKEUP command to the specified slave
  538. * @dev: SPMI device.
  539. * @sid: slave identifier.
  540. *
  541. * The Wakeup command causes the Slave to move from the SLEEP state to
  542. * the ACTIVE state.
  543. *
  544. * Returns
  545. * -EINVAL for invalid Slave Identifier.
  546. * -EPERM if the SPMI transaction is denied due to permission issues.
  547. * -EIO if the SPMI transaction fails (parity errors, etc).
  548. * -ETIMEDOUT if the SPMI transaction times out.
  549. */
  550. int spmi_command_wakeup(struct spmi_controller *ctrl, u8 sid)
  551. {
  552. if (sid > SPMI_MAX_SLAVE_ID)
  553. return -EINVAL;
  554. return spmi_cmd(ctrl, SPMI_CMD_WAKEUP, sid);
  555. }
  556. EXPORT_SYMBOL_GPL(spmi_command_wakeup);
  557. /**
  558. * spmi_command_shutdown() - sends SHUTDOWN command to the specified slave
  559. * @dev: SPMI device.
  560. * @sid: slave identifier.
  561. *
  562. * The Shutdown command causes the Slave to enter the SHUTDOWN state.
  563. *
  564. * Returns
  565. * -EINVAL for invalid Slave Identifier.
  566. * -EPERM if the SPMI transaction is denied due to permission issues.
  567. * -EIO if the SPMI transaction fails (parity errors, etc).
  568. * -ETIMEDOUT if the SPMI transaction times out.
  569. */
  570. int spmi_command_shutdown(struct spmi_controller *ctrl, u8 sid)
  571. {
  572. if (sid > SPMI_MAX_SLAVE_ID)
  573. return -EINVAL;
  574. return spmi_cmd(ctrl, SPMI_CMD_SHUTDOWN, sid);
  575. }
  576. EXPORT_SYMBOL_GPL(spmi_command_shutdown);
  577. /* ------------------------------------------------------------------------- */
  578. static const struct spmi_device_id *spmi_match(const struct spmi_device_id *id,
  579. const struct spmi_device *spmi_dev)
  580. {
  581. while (id->name[0]) {
  582. if (strncmp(spmi_dev->name, id->name, SPMI_NAME_SIZE) == 0)
  583. return id;
  584. id++;
  585. }
  586. return NULL;
  587. }
  588. static int spmi_device_match(struct device *dev, struct device_driver *drv)
  589. {
  590. struct spmi_device *spmi_dev;
  591. struct spmi_driver *sdrv = to_spmi_driver(drv);
  592. if (dev->type == &spmi_dev_type)
  593. spmi_dev = to_spmi_device(dev);
  594. else
  595. return 0;
  596. /* Attempt an OF style match */
  597. if (of_driver_match_device(dev, drv))
  598. return 1;
  599. if (sdrv->id_table)
  600. return spmi_match(sdrv->id_table, spmi_dev) != NULL;
  601. if (drv->name)
  602. return strncmp(spmi_dev->name, drv->name, SPMI_NAME_SIZE) == 0;
  603. return 0;
  604. }
  605. #ifdef CONFIG_PM_SLEEP
  606. static int spmi_legacy_suspend(struct device *dev, pm_message_t mesg)
  607. {
  608. struct spmi_device *spmi_dev = NULL;
  609. struct spmi_driver *driver;
  610. if (dev->type == &spmi_dev_type)
  611. spmi_dev = to_spmi_device(dev);
  612. if (!spmi_dev || !dev->driver)
  613. return 0;
  614. driver = to_spmi_driver(dev->driver);
  615. if (!driver->suspend)
  616. return 0;
  617. return driver->suspend(spmi_dev, mesg);
  618. }
  619. static int spmi_legacy_resume(struct device *dev)
  620. {
  621. struct spmi_device *spmi_dev = NULL;
  622. struct spmi_driver *driver;
  623. if (dev->type == &spmi_dev_type)
  624. spmi_dev = to_spmi_device(dev);
  625. if (!spmi_dev || !dev->driver)
  626. return 0;
  627. driver = to_spmi_driver(dev->driver);
  628. if (!driver->resume)
  629. return 0;
  630. return driver->resume(spmi_dev);
  631. }
  632. static int spmi_pm_suspend(struct device *dev)
  633. {
  634. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  635. if (pm)
  636. return pm_generic_suspend(dev);
  637. else
  638. return spmi_legacy_suspend(dev, PMSG_SUSPEND);
  639. }
  640. static int spmi_pm_resume(struct device *dev)
  641. {
  642. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  643. if (pm)
  644. return pm_generic_resume(dev);
  645. else
  646. return spmi_legacy_resume(dev);
  647. }
  648. #else
  649. #define spmi_pm_suspend NULL
  650. #define spmi_pm_resume NULL
  651. #endif
  652. static const struct dev_pm_ops spmi_pm_ops = {
  653. .suspend = spmi_pm_suspend,
  654. .resume = spmi_pm_resume,
  655. SET_RUNTIME_PM_OPS(
  656. pm_generic_suspend,
  657. pm_generic_resume,
  658. pm_generic_runtime_idle
  659. )
  660. };
  661. struct bus_type spmi_bus_type = {
  662. .name = "spmi",
  663. .match = spmi_device_match,
  664. .pm = &spmi_pm_ops,
  665. };
  666. EXPORT_SYMBOL_GPL(spmi_bus_type);
  667. struct device spmi_dev = {
  668. .init_name = "spmi",
  669. };
  670. static int spmi_drv_probe(struct device *dev)
  671. {
  672. const struct spmi_driver *sdrv = to_spmi_driver(dev->driver);
  673. return sdrv->probe(to_spmi_device(dev));
  674. }
  675. static int spmi_drv_remove(struct device *dev)
  676. {
  677. const struct spmi_driver *sdrv = to_spmi_driver(dev->driver);
  678. return sdrv->remove(to_spmi_device(dev));
  679. }
  680. static void spmi_drv_shutdown(struct device *dev)
  681. {
  682. const struct spmi_driver *sdrv = to_spmi_driver(dev->driver);
  683. sdrv->shutdown(to_spmi_device(dev));
  684. }
  685. /**
  686. * spmi_driver_register: Client driver registration with SPMI framework.
  687. * @drv: client driver to be associated with client-device.
  688. *
  689. * This API will register the client driver with the SPMI framework.
  690. * It is called from the driver's module-init function.
  691. */
  692. int spmi_driver_register(struct spmi_driver *drv)
  693. {
  694. drv->driver.bus = &spmi_bus_type;
  695. if (drv->probe)
  696. drv->driver.probe = spmi_drv_probe;
  697. if (drv->remove)
  698. drv->driver.remove = spmi_drv_remove;
  699. if (drv->shutdown)
  700. drv->driver.shutdown = spmi_drv_shutdown;
  701. return driver_register(&drv->driver);
  702. }
  703. EXPORT_SYMBOL_GPL(spmi_driver_register);
  704. static int spmi_register_controller(struct spmi_controller *ctrl)
  705. {
  706. int ret = 0;
  707. /* Can't register until after driver model init */
  708. if (WARN_ON(!spmi_bus_type.p)) {
  709. ret = -EAGAIN;
  710. goto exit;
  711. }
  712. dev_set_name(&ctrl->dev, "spmi-%d", ctrl->nr);
  713. ctrl->dev.bus = &spmi_bus_type;
  714. ctrl->dev.type = &spmi_ctrl_type;
  715. ret = device_register(&ctrl->dev);
  716. if (ret)
  717. goto exit;
  718. dev_dbg(&ctrl->dev, "Bus spmi-%d registered: dev:0x%p\n",
  719. ctrl->nr, &ctrl->dev);
  720. #if (!defined(CONFIG_MACH_CT01) && !defined(CONFIG_MACH_CT01_CHN_CU))
  721. /* make global controller */
  722. spmi_ctrl_extra = ctrl;
  723. #endif
  724. spmi_dfs_add_controller(ctrl);
  725. return 0;
  726. exit:
  727. mutex_lock(&board_lock);
  728. idr_remove(&ctrl_idr, ctrl->nr);
  729. mutex_unlock(&board_lock);
  730. return ret;
  731. }
  732. static void __exit spmi_exit(void)
  733. {
  734. device_unregister(&spmi_dev);
  735. bus_unregister(&spmi_bus_type);
  736. }
  737. static int __init spmi_init(void)
  738. {
  739. int retval;
  740. retval = bus_register(&spmi_bus_type);
  741. if (!retval)
  742. retval = device_register(&spmi_dev);
  743. if (retval)
  744. bus_unregister(&spmi_bus_type);
  745. return retval;
  746. }
  747. postcore_initcall(spmi_init);
  748. module_exit(spmi_exit);
  749. MODULE_LICENSE("GPL v2");
  750. MODULE_VERSION("1.0");
  751. MODULE_DESCRIPTION("SPMI module");
  752. MODULE_ALIAS("platform:spmi");