sunxi-rsb.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * RSB (Reduced Serial Bus) driver.
  4. *
  5. * Author: Chen-Yu Tsai <wens@csie.org>
  6. *
  7. * The RSB controller looks like an SMBus controller which only supports
  8. * byte and word data transfers. But, it differs from standard SMBus
  9. * protocol on several aspects:
  10. * - it uses addresses set at runtime to address slaves. Runtime addresses
  11. * are sent to slaves using their 12bit hardware addresses. Up to 15
  12. * runtime addresses are available.
  13. * - it adds a parity bit every 8bits of data and address for read and
  14. * write accesses; this replaces the ack bit
  15. * - only one read access is required to read a byte (instead of a write
  16. * followed by a read access in standard SMBus protocol)
  17. * - there's no Ack bit after each read access
  18. *
  19. * This means this bus cannot be used to interface with standard SMBus
  20. * devices. Devices known to support this interface include the AXP223,
  21. * AXP809, and AXP806 PMICs, and the AC100 audio codec, all from X-Powers.
  22. *
  23. * A description of the operation and wire protocol can be found in the
  24. * RSB section of Allwinner's A80 user manual, which can be found at
  25. *
  26. * https://github.com/allwinner-zh/documents/tree/master/A80
  27. *
  28. * This document is officially released by Allwinner.
  29. *
  30. * This driver is based on i2c-sun6i-p2wi.c, the P2WI bus driver.
  31. */
  32. #include <linux/clk.h>
  33. #include <linux/clk/clk-conf.h>
  34. #include <linux/device.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/io.h>
  37. #include <linux/iopoll.h>
  38. #include <linux/module.h>
  39. #include <linux/of.h>
  40. #include <linux/of_irq.h>
  41. #include <linux/of_device.h>
  42. #include <linux/platform_device.h>
  43. #include <linux/pm.h>
  44. #include <linux/pm_runtime.h>
  45. #include <linux/regmap.h>
  46. #include <linux/reset.h>
  47. #include <linux/slab.h>
  48. #include <linux/sunxi-rsb.h>
  49. #include <linux/types.h>
  50. /* RSB registers */
  51. #define RSB_CTRL 0x0 /* Global control */
  52. #define RSB_CCR 0x4 /* Clock control */
  53. #define RSB_INTE 0x8 /* Interrupt controls */
  54. #define RSB_INTS 0xc /* Interrupt status */
  55. #define RSB_ADDR 0x10 /* Address to send with read/write command */
  56. #define RSB_DATA 0x1c /* Data to read/write */
  57. #define RSB_LCR 0x24 /* Line control */
  58. #define RSB_DMCR 0x28 /* Device mode (init) control */
  59. #define RSB_CMD 0x2c /* RSB Command */
  60. #define RSB_DAR 0x30 /* Device address / runtime address */
  61. /* CTRL fields */
  62. #define RSB_CTRL_START_TRANS BIT(7)
  63. #define RSB_CTRL_ABORT_TRANS BIT(6)
  64. #define RSB_CTRL_GLOBAL_INT_ENB BIT(1)
  65. #define RSB_CTRL_SOFT_RST BIT(0)
  66. /* CLK CTRL fields */
  67. #define RSB_CCR_SDA_OUT_DELAY(v) (((v) & 0x7) << 8)
  68. #define RSB_CCR_MAX_CLK_DIV 0xff
  69. #define RSB_CCR_CLK_DIV(v) ((v) & RSB_CCR_MAX_CLK_DIV)
  70. /* STATUS fields */
  71. #define RSB_INTS_TRANS_ERR_ACK BIT(16)
  72. #define RSB_INTS_TRANS_ERR_DATA_BIT(v) (((v) >> 8) & 0xf)
  73. #define RSB_INTS_TRANS_ERR_DATA GENMASK(11, 8)
  74. #define RSB_INTS_LOAD_BSY BIT(2)
  75. #define RSB_INTS_TRANS_ERR BIT(1)
  76. #define RSB_INTS_TRANS_OVER BIT(0)
  77. /* LINE CTRL fields*/
  78. #define RSB_LCR_SCL_STATE BIT(5)
  79. #define RSB_LCR_SDA_STATE BIT(4)
  80. #define RSB_LCR_SCL_CTL BIT(3)
  81. #define RSB_LCR_SCL_CTL_EN BIT(2)
  82. #define RSB_LCR_SDA_CTL BIT(1)
  83. #define RSB_LCR_SDA_CTL_EN BIT(0)
  84. /* DEVICE MODE CTRL field values */
  85. #define RSB_DMCR_DEVICE_START BIT(31)
  86. #define RSB_DMCR_MODE_DATA (0x7c << 16)
  87. #define RSB_DMCR_MODE_REG (0x3e << 8)
  88. #define RSB_DMCR_DEV_ADDR 0x00
  89. /* CMD values */
  90. #define RSB_CMD_RD8 0x8b
  91. #define RSB_CMD_RD16 0x9c
  92. #define RSB_CMD_RD32 0xa6
  93. #define RSB_CMD_WR8 0x4e
  94. #define RSB_CMD_WR16 0x59
  95. #define RSB_CMD_WR32 0x63
  96. #define RSB_CMD_STRA 0xe8
  97. /* DAR fields */
  98. #define RSB_DAR_RTA(v) (((v) & 0xff) << 16)
  99. #define RSB_DAR_DA(v) ((v) & 0xffff)
  100. #define RSB_MAX_FREQ 20000000
  101. #define RSB_CTRL_NAME "sunxi-rsb"
  102. struct sunxi_rsb_addr_map {
  103. u16 hwaddr;
  104. u8 rtaddr;
  105. };
  106. struct sunxi_rsb {
  107. struct device *dev;
  108. void __iomem *regs;
  109. struct clk *clk;
  110. struct reset_control *rstc;
  111. struct completion complete;
  112. struct mutex lock;
  113. unsigned int status;
  114. u32 clk_freq;
  115. };
  116. /* bus / slave device related functions */
  117. static const struct bus_type sunxi_rsb_bus;
  118. static int sunxi_rsb_device_match(struct device *dev, const struct device_driver *drv)
  119. {
  120. return of_driver_match_device(dev, drv);
  121. }
  122. static int sunxi_rsb_device_probe(struct device *dev)
  123. {
  124. const struct sunxi_rsb_driver *drv = to_sunxi_rsb_driver(dev->driver);
  125. struct sunxi_rsb_device *rdev = to_sunxi_rsb_device(dev);
  126. int ret;
  127. if (!drv->probe)
  128. return -ENODEV;
  129. if (!rdev->irq) {
  130. int irq = -ENOENT;
  131. if (dev->of_node)
  132. irq = of_irq_get(dev->of_node, 0);
  133. if (irq == -EPROBE_DEFER)
  134. return irq;
  135. if (irq < 0)
  136. irq = 0;
  137. rdev->irq = irq;
  138. }
  139. ret = of_clk_set_defaults(dev->of_node, false);
  140. if (ret < 0)
  141. return ret;
  142. return drv->probe(rdev);
  143. }
  144. static void sunxi_rsb_device_remove(struct device *dev)
  145. {
  146. const struct sunxi_rsb_driver *drv = to_sunxi_rsb_driver(dev->driver);
  147. drv->remove(to_sunxi_rsb_device(dev));
  148. }
  149. static int sunxi_rsb_device_modalias(const struct device *dev, struct kobj_uevent_env *env)
  150. {
  151. return of_device_uevent_modalias(dev, env);
  152. }
  153. static const struct bus_type sunxi_rsb_bus = {
  154. .name = RSB_CTRL_NAME,
  155. .match = sunxi_rsb_device_match,
  156. .probe = sunxi_rsb_device_probe,
  157. .remove = sunxi_rsb_device_remove,
  158. .uevent = sunxi_rsb_device_modalias,
  159. };
  160. static void sunxi_rsb_dev_release(struct device *dev)
  161. {
  162. struct sunxi_rsb_device *rdev = to_sunxi_rsb_device(dev);
  163. kfree(rdev);
  164. }
  165. /**
  166. * sunxi_rsb_device_create() - allocate and add an RSB device
  167. * @rsb: RSB controller
  168. * @node: RSB slave device node
  169. * @hwaddr: RSB slave hardware address
  170. * @rtaddr: RSB slave runtime address
  171. */
  172. static struct sunxi_rsb_device *sunxi_rsb_device_create(struct sunxi_rsb *rsb,
  173. struct device_node *node, u16 hwaddr, u8 rtaddr)
  174. {
  175. int err;
  176. struct sunxi_rsb_device *rdev;
  177. rdev = kzalloc_obj(*rdev);
  178. if (!rdev)
  179. return ERR_PTR(-ENOMEM);
  180. rdev->rsb = rsb;
  181. rdev->hwaddr = hwaddr;
  182. rdev->rtaddr = rtaddr;
  183. rdev->dev.bus = &sunxi_rsb_bus;
  184. rdev->dev.parent = rsb->dev;
  185. rdev->dev.of_node = node;
  186. rdev->dev.release = sunxi_rsb_dev_release;
  187. dev_set_name(&rdev->dev, "%s-%x", RSB_CTRL_NAME, hwaddr);
  188. err = device_register(&rdev->dev);
  189. if (err < 0) {
  190. dev_err(&rdev->dev, "Can't add %s, status %d\n",
  191. dev_name(&rdev->dev), err);
  192. goto err_device_add;
  193. }
  194. dev_dbg(&rdev->dev, "device %s registered\n", dev_name(&rdev->dev));
  195. return rdev;
  196. err_device_add:
  197. put_device(&rdev->dev);
  198. return ERR_PTR(err);
  199. }
  200. /**
  201. * sunxi_rsb_device_unregister(): unregister an RSB device
  202. * @rdev: rsb_device to be removed
  203. */
  204. static void sunxi_rsb_device_unregister(struct sunxi_rsb_device *rdev)
  205. {
  206. device_unregister(&rdev->dev);
  207. }
  208. static int sunxi_rsb_remove_devices(struct device *dev, void *data)
  209. {
  210. struct sunxi_rsb_device *rdev = to_sunxi_rsb_device(dev);
  211. if (dev->bus == &sunxi_rsb_bus)
  212. sunxi_rsb_device_unregister(rdev);
  213. return 0;
  214. }
  215. /**
  216. * sunxi_rsb_driver_register() - Register device driver with RSB core
  217. * @rdrv: device driver to be associated with slave-device.
  218. *
  219. * This API will register the client driver with the RSB framework.
  220. * It is typically called from the driver's module-init function.
  221. */
  222. int sunxi_rsb_driver_register(struct sunxi_rsb_driver *rdrv)
  223. {
  224. rdrv->driver.bus = &sunxi_rsb_bus;
  225. return driver_register(&rdrv->driver);
  226. }
  227. EXPORT_SYMBOL_GPL(sunxi_rsb_driver_register);
  228. /* common code that starts a transfer */
  229. static int _sunxi_rsb_run_xfer(struct sunxi_rsb *rsb)
  230. {
  231. u32 int_mask, status;
  232. bool timeout;
  233. if (readl(rsb->regs + RSB_CTRL) & RSB_CTRL_START_TRANS) {
  234. dev_dbg(rsb->dev, "RSB transfer still in progress\n");
  235. return -EBUSY;
  236. }
  237. reinit_completion(&rsb->complete);
  238. int_mask = RSB_INTS_LOAD_BSY | RSB_INTS_TRANS_ERR | RSB_INTS_TRANS_OVER;
  239. writel(int_mask, rsb->regs + RSB_INTE);
  240. writel(RSB_CTRL_START_TRANS | RSB_CTRL_GLOBAL_INT_ENB,
  241. rsb->regs + RSB_CTRL);
  242. if (irqs_disabled()) {
  243. timeout = readl_poll_timeout_atomic(rsb->regs + RSB_INTS,
  244. status, (status & int_mask),
  245. 10, 100000);
  246. writel(status, rsb->regs + RSB_INTS);
  247. } else {
  248. timeout = !wait_for_completion_io_timeout(&rsb->complete,
  249. msecs_to_jiffies(100));
  250. status = rsb->status;
  251. }
  252. if (timeout) {
  253. dev_dbg(rsb->dev, "RSB timeout\n");
  254. /* abort the transfer */
  255. writel(RSB_CTRL_ABORT_TRANS, rsb->regs + RSB_CTRL);
  256. /* clear any interrupt flags */
  257. writel(readl(rsb->regs + RSB_INTS), rsb->regs + RSB_INTS);
  258. return -ETIMEDOUT;
  259. }
  260. if (status & RSB_INTS_LOAD_BSY) {
  261. dev_dbg(rsb->dev, "RSB busy\n");
  262. return -EBUSY;
  263. }
  264. if (status & RSB_INTS_TRANS_ERR) {
  265. if (status & RSB_INTS_TRANS_ERR_ACK) {
  266. dev_dbg(rsb->dev, "RSB slave nack\n");
  267. return -EINVAL;
  268. }
  269. if (status & RSB_INTS_TRANS_ERR_DATA) {
  270. dev_dbg(rsb->dev, "RSB transfer data error\n");
  271. return -EIO;
  272. }
  273. }
  274. return 0;
  275. }
  276. static int sunxi_rsb_read(struct sunxi_rsb *rsb, u8 rtaddr, u8 addr,
  277. u32 *buf, size_t len)
  278. {
  279. u32 cmd;
  280. int ret;
  281. if (!buf)
  282. return -EINVAL;
  283. switch (len) {
  284. case 1:
  285. cmd = RSB_CMD_RD8;
  286. break;
  287. case 2:
  288. cmd = RSB_CMD_RD16;
  289. break;
  290. case 4:
  291. cmd = RSB_CMD_RD32;
  292. break;
  293. default:
  294. dev_err(rsb->dev, "Invalid access width: %zd\n", len);
  295. return -EINVAL;
  296. }
  297. ret = pm_runtime_resume_and_get(rsb->dev);
  298. if (ret)
  299. return ret;
  300. mutex_lock(&rsb->lock);
  301. writel(addr, rsb->regs + RSB_ADDR);
  302. writel(RSB_DAR_RTA(rtaddr), rsb->regs + RSB_DAR);
  303. writel(cmd, rsb->regs + RSB_CMD);
  304. ret = _sunxi_rsb_run_xfer(rsb);
  305. if (ret)
  306. goto unlock;
  307. *buf = readl(rsb->regs + RSB_DATA) & GENMASK(len * 8 - 1, 0);
  308. unlock:
  309. mutex_unlock(&rsb->lock);
  310. pm_runtime_put_autosuspend(rsb->dev);
  311. return ret;
  312. }
  313. static int sunxi_rsb_write(struct sunxi_rsb *rsb, u8 rtaddr, u8 addr,
  314. const u32 *buf, size_t len)
  315. {
  316. u32 cmd;
  317. int ret;
  318. if (!buf)
  319. return -EINVAL;
  320. switch (len) {
  321. case 1:
  322. cmd = RSB_CMD_WR8;
  323. break;
  324. case 2:
  325. cmd = RSB_CMD_WR16;
  326. break;
  327. case 4:
  328. cmd = RSB_CMD_WR32;
  329. break;
  330. default:
  331. dev_err(rsb->dev, "Invalid access width: %zd\n", len);
  332. return -EINVAL;
  333. }
  334. ret = pm_runtime_resume_and_get(rsb->dev);
  335. if (ret)
  336. return ret;
  337. mutex_lock(&rsb->lock);
  338. writel(addr, rsb->regs + RSB_ADDR);
  339. writel(RSB_DAR_RTA(rtaddr), rsb->regs + RSB_DAR);
  340. writel(*buf, rsb->regs + RSB_DATA);
  341. writel(cmd, rsb->regs + RSB_CMD);
  342. ret = _sunxi_rsb_run_xfer(rsb);
  343. mutex_unlock(&rsb->lock);
  344. pm_runtime_put_autosuspend(rsb->dev);
  345. return ret;
  346. }
  347. /* RSB regmap functions */
  348. struct sunxi_rsb_ctx {
  349. struct sunxi_rsb_device *rdev;
  350. int size;
  351. };
  352. static int regmap_sunxi_rsb_reg_read(void *context, unsigned int reg,
  353. unsigned int *val)
  354. {
  355. struct sunxi_rsb_ctx *ctx = context;
  356. struct sunxi_rsb_device *rdev = ctx->rdev;
  357. if (reg > 0xff)
  358. return -EINVAL;
  359. return sunxi_rsb_read(rdev->rsb, rdev->rtaddr, reg, val, ctx->size);
  360. }
  361. static int regmap_sunxi_rsb_reg_write(void *context, unsigned int reg,
  362. unsigned int val)
  363. {
  364. struct sunxi_rsb_ctx *ctx = context;
  365. struct sunxi_rsb_device *rdev = ctx->rdev;
  366. return sunxi_rsb_write(rdev->rsb, rdev->rtaddr, reg, &val, ctx->size);
  367. }
  368. static void regmap_sunxi_rsb_free_ctx(void *context)
  369. {
  370. struct sunxi_rsb_ctx *ctx = context;
  371. kfree(ctx);
  372. }
  373. static const struct regmap_bus regmap_sunxi_rsb = {
  374. .reg_write = regmap_sunxi_rsb_reg_write,
  375. .reg_read = regmap_sunxi_rsb_reg_read,
  376. .free_context = regmap_sunxi_rsb_free_ctx,
  377. .reg_format_endian_default = REGMAP_ENDIAN_NATIVE,
  378. .val_format_endian_default = REGMAP_ENDIAN_NATIVE,
  379. };
  380. static struct sunxi_rsb_ctx *regmap_sunxi_rsb_init_ctx(struct sunxi_rsb_device *rdev,
  381. const struct regmap_config *config)
  382. {
  383. struct sunxi_rsb_ctx *ctx;
  384. switch (config->val_bits) {
  385. case 8:
  386. case 16:
  387. case 32:
  388. break;
  389. default:
  390. return ERR_PTR(-EINVAL);
  391. }
  392. ctx = kzalloc_obj(*ctx);
  393. if (!ctx)
  394. return ERR_PTR(-ENOMEM);
  395. ctx->rdev = rdev;
  396. ctx->size = config->val_bits / 8;
  397. return ctx;
  398. }
  399. struct regmap *__devm_regmap_init_sunxi_rsb(struct sunxi_rsb_device *rdev,
  400. const struct regmap_config *config,
  401. struct lock_class_key *lock_key,
  402. const char *lock_name)
  403. {
  404. struct sunxi_rsb_ctx *ctx = regmap_sunxi_rsb_init_ctx(rdev, config);
  405. if (IS_ERR(ctx))
  406. return ERR_CAST(ctx);
  407. return __devm_regmap_init(&rdev->dev, &regmap_sunxi_rsb, ctx, config,
  408. lock_key, lock_name);
  409. }
  410. EXPORT_SYMBOL_GPL(__devm_regmap_init_sunxi_rsb);
  411. /* RSB controller driver functions */
  412. static irqreturn_t sunxi_rsb_irq(int irq, void *dev_id)
  413. {
  414. struct sunxi_rsb *rsb = dev_id;
  415. u32 status;
  416. status = readl(rsb->regs + RSB_INTS);
  417. rsb->status = status;
  418. /* Clear interrupts */
  419. status &= (RSB_INTS_LOAD_BSY | RSB_INTS_TRANS_ERR |
  420. RSB_INTS_TRANS_OVER);
  421. writel(status, rsb->regs + RSB_INTS);
  422. complete(&rsb->complete);
  423. return IRQ_HANDLED;
  424. }
  425. static int sunxi_rsb_init_device_mode(struct sunxi_rsb *rsb)
  426. {
  427. int ret = 0;
  428. u32 reg;
  429. /* send init sequence */
  430. writel(RSB_DMCR_DEVICE_START | RSB_DMCR_MODE_DATA |
  431. RSB_DMCR_MODE_REG | RSB_DMCR_DEV_ADDR, rsb->regs + RSB_DMCR);
  432. readl_poll_timeout(rsb->regs + RSB_DMCR, reg,
  433. !(reg & RSB_DMCR_DEVICE_START), 100, 250000);
  434. if (reg & RSB_DMCR_DEVICE_START)
  435. ret = -ETIMEDOUT;
  436. /* clear interrupt status bits */
  437. writel(readl(rsb->regs + RSB_INTS), rsb->regs + RSB_INTS);
  438. return ret;
  439. }
  440. /*
  441. * There are 15 valid runtime addresses, though Allwinner typically
  442. * skips the first, for unknown reasons, and uses the following three.
  443. *
  444. * 0x17, 0x2d, 0x3a, 0x4e, 0x59, 0x63, 0x74, 0x8b,
  445. * 0x9c, 0xa6, 0xb1, 0xc5, 0xd2, 0xe8, 0xff
  446. *
  447. * No designs with 2 RSB slave devices sharing identical hardware
  448. * addresses on the same bus have been seen in the wild. All designs
  449. * use 0x2d for the primary PMIC, 0x3a for the secondary PMIC if
  450. * there is one, and 0x45 for peripheral ICs.
  451. *
  452. * The hardware does not seem to support re-setting runtime addresses.
  453. * Attempts to do so result in the slave devices returning a NACK.
  454. * Hence we just hardcode the mapping here, like Allwinner does.
  455. */
  456. static const struct sunxi_rsb_addr_map sunxi_rsb_addr_maps[] = {
  457. { 0x3a3, 0x2d }, /* Primary PMIC: AXP223, AXP809, AXP81X, ... */
  458. { 0x745, 0x3a }, /* Secondary PMIC: AXP806, ... */
  459. { 0xe89, 0x4e }, /* Peripheral IC: AC100, ... */
  460. };
  461. static u8 sunxi_rsb_get_rtaddr(u16 hwaddr)
  462. {
  463. int i;
  464. for (i = 0; i < ARRAY_SIZE(sunxi_rsb_addr_maps); i++)
  465. if (hwaddr == sunxi_rsb_addr_maps[i].hwaddr)
  466. return sunxi_rsb_addr_maps[i].rtaddr;
  467. return 0; /* 0 is an invalid runtime address */
  468. }
  469. static int of_rsb_register_devices(struct sunxi_rsb *rsb)
  470. {
  471. struct device *dev = rsb->dev;
  472. struct device_node *child, *np = dev->of_node;
  473. u32 hwaddr;
  474. u8 rtaddr;
  475. int ret;
  476. if (!np)
  477. return -EINVAL;
  478. /* Runtime addresses for all slaves should be set first */
  479. for_each_available_child_of_node(np, child) {
  480. dev_dbg(dev, "setting child %pOF runtime address\n",
  481. child);
  482. ret = of_property_read_u32(child, "reg", &hwaddr);
  483. if (ret) {
  484. dev_err(dev, "%pOF: invalid 'reg' property: %d\n",
  485. child, ret);
  486. continue;
  487. }
  488. rtaddr = sunxi_rsb_get_rtaddr(hwaddr);
  489. if (!rtaddr) {
  490. dev_err(dev, "%pOF: unknown hardware device address\n",
  491. child);
  492. continue;
  493. }
  494. /*
  495. * Since no devices have been registered yet, we are the
  496. * only ones using the bus, we can skip locking the bus.
  497. */
  498. /* setup command parameters */
  499. writel(RSB_CMD_STRA, rsb->regs + RSB_CMD);
  500. writel(RSB_DAR_RTA(rtaddr) | RSB_DAR_DA(hwaddr),
  501. rsb->regs + RSB_DAR);
  502. /* send command */
  503. ret = _sunxi_rsb_run_xfer(rsb);
  504. if (ret)
  505. dev_warn(dev, "%pOF: set runtime address failed: %d\n",
  506. child, ret);
  507. }
  508. /* Then we start adding devices and probing them */
  509. for_each_available_child_of_node(np, child) {
  510. struct sunxi_rsb_device *rdev;
  511. dev_dbg(dev, "adding child %pOF\n", child);
  512. ret = of_property_read_u32(child, "reg", &hwaddr);
  513. if (ret)
  514. continue;
  515. rtaddr = sunxi_rsb_get_rtaddr(hwaddr);
  516. if (!rtaddr)
  517. continue;
  518. rdev = sunxi_rsb_device_create(rsb, child, hwaddr, rtaddr);
  519. if (IS_ERR(rdev))
  520. dev_err(dev, "failed to add child device %pOF: %ld\n",
  521. child, PTR_ERR(rdev));
  522. }
  523. return 0;
  524. }
  525. static int sunxi_rsb_hw_init(struct sunxi_rsb *rsb)
  526. {
  527. struct device *dev = rsb->dev;
  528. unsigned long p_clk_freq;
  529. u32 clk_delay, reg;
  530. int clk_div, ret;
  531. ret = clk_prepare_enable(rsb->clk);
  532. if (ret) {
  533. dev_err(dev, "failed to enable clk: %d\n", ret);
  534. return ret;
  535. }
  536. ret = reset_control_deassert(rsb->rstc);
  537. if (ret) {
  538. dev_err(dev, "failed to deassert reset line: %d\n", ret);
  539. goto err_clk_disable;
  540. }
  541. /* reset the controller */
  542. writel(RSB_CTRL_SOFT_RST, rsb->regs + RSB_CTRL);
  543. readl_poll_timeout(rsb->regs + RSB_CTRL, reg,
  544. !(reg & RSB_CTRL_SOFT_RST), 1000, 100000);
  545. /*
  546. * Clock frequency and delay calculation code is from
  547. * Allwinner U-boot sources.
  548. *
  549. * From A83 user manual:
  550. * bus clock frequency = parent clock frequency / (2 * (divider + 1))
  551. */
  552. p_clk_freq = clk_get_rate(rsb->clk);
  553. clk_div = p_clk_freq / rsb->clk_freq / 2;
  554. if (!clk_div)
  555. clk_div = 1;
  556. else if (clk_div > RSB_CCR_MAX_CLK_DIV + 1)
  557. clk_div = RSB_CCR_MAX_CLK_DIV + 1;
  558. clk_delay = clk_div >> 1;
  559. if (!clk_delay)
  560. clk_delay = 1;
  561. dev_info(dev, "RSB running at %lu Hz\n", p_clk_freq / clk_div / 2);
  562. writel(RSB_CCR_SDA_OUT_DELAY(clk_delay) | RSB_CCR_CLK_DIV(clk_div - 1),
  563. rsb->regs + RSB_CCR);
  564. return 0;
  565. err_clk_disable:
  566. clk_disable_unprepare(rsb->clk);
  567. return ret;
  568. }
  569. static void sunxi_rsb_hw_exit(struct sunxi_rsb *rsb)
  570. {
  571. reset_control_assert(rsb->rstc);
  572. /* Keep the clock and PM reference counts consistent. */
  573. if (!pm_runtime_status_suspended(rsb->dev))
  574. clk_disable_unprepare(rsb->clk);
  575. }
  576. static int __maybe_unused sunxi_rsb_runtime_suspend(struct device *dev)
  577. {
  578. struct sunxi_rsb *rsb = dev_get_drvdata(dev);
  579. clk_disable_unprepare(rsb->clk);
  580. return 0;
  581. }
  582. static int __maybe_unused sunxi_rsb_runtime_resume(struct device *dev)
  583. {
  584. struct sunxi_rsb *rsb = dev_get_drvdata(dev);
  585. return clk_prepare_enable(rsb->clk);
  586. }
  587. static int __maybe_unused sunxi_rsb_suspend(struct device *dev)
  588. {
  589. struct sunxi_rsb *rsb = dev_get_drvdata(dev);
  590. sunxi_rsb_hw_exit(rsb);
  591. return 0;
  592. }
  593. static int __maybe_unused sunxi_rsb_resume(struct device *dev)
  594. {
  595. struct sunxi_rsb *rsb = dev_get_drvdata(dev);
  596. return sunxi_rsb_hw_init(rsb);
  597. }
  598. static int sunxi_rsb_probe(struct platform_device *pdev)
  599. {
  600. struct device *dev = &pdev->dev;
  601. struct device_node *np = dev->of_node;
  602. struct sunxi_rsb *rsb;
  603. u32 clk_freq = 3000000;
  604. int irq, ret;
  605. of_property_read_u32(np, "clock-frequency", &clk_freq);
  606. if (clk_freq > RSB_MAX_FREQ)
  607. return dev_err_probe(dev, -EINVAL,
  608. "clock-frequency (%u Hz) is too high (max = 20MHz)\n",
  609. clk_freq);
  610. rsb = devm_kzalloc(dev, sizeof(*rsb), GFP_KERNEL);
  611. if (!rsb)
  612. return -ENOMEM;
  613. rsb->dev = dev;
  614. rsb->clk_freq = clk_freq;
  615. platform_set_drvdata(pdev, rsb);
  616. rsb->regs = devm_platform_ioremap_resource(pdev, 0);
  617. if (IS_ERR(rsb->regs))
  618. return PTR_ERR(rsb->regs);
  619. irq = platform_get_irq(pdev, 0);
  620. if (irq < 0)
  621. return irq;
  622. rsb->clk = devm_clk_get(dev, NULL);
  623. if (IS_ERR(rsb->clk))
  624. return dev_err_probe(dev, PTR_ERR(rsb->clk),
  625. "failed to retrieve clk\n");
  626. rsb->rstc = devm_reset_control_get(dev, NULL);
  627. if (IS_ERR(rsb->rstc))
  628. return dev_err_probe(dev, PTR_ERR(rsb->rstc),
  629. "failed to retrieve reset controller\n");
  630. init_completion(&rsb->complete);
  631. mutex_init(&rsb->lock);
  632. ret = devm_request_irq(dev, irq, sunxi_rsb_irq, 0, RSB_CTRL_NAME, rsb);
  633. if (ret)
  634. return dev_err_probe(dev, ret,
  635. "can't register interrupt handler irq %d\n", irq);
  636. ret = sunxi_rsb_hw_init(rsb);
  637. if (ret)
  638. return ret;
  639. /* initialize all devices on the bus into RSB mode */
  640. ret = sunxi_rsb_init_device_mode(rsb);
  641. if (ret)
  642. dev_warn(dev, "Initialize device mode failed: %d\n", ret);
  643. pm_suspend_ignore_children(dev, true);
  644. pm_runtime_set_active(dev);
  645. pm_runtime_set_autosuspend_delay(dev, MSEC_PER_SEC);
  646. pm_runtime_use_autosuspend(dev);
  647. pm_runtime_enable(dev);
  648. of_rsb_register_devices(rsb);
  649. return 0;
  650. }
  651. static void sunxi_rsb_remove(struct platform_device *pdev)
  652. {
  653. struct sunxi_rsb *rsb = platform_get_drvdata(pdev);
  654. device_for_each_child(rsb->dev, NULL, sunxi_rsb_remove_devices);
  655. pm_runtime_disable(&pdev->dev);
  656. sunxi_rsb_hw_exit(rsb);
  657. }
  658. static const struct dev_pm_ops sunxi_rsb_dev_pm_ops = {
  659. SET_RUNTIME_PM_OPS(sunxi_rsb_runtime_suspend,
  660. sunxi_rsb_runtime_resume, NULL)
  661. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sunxi_rsb_suspend, sunxi_rsb_resume)
  662. };
  663. static const struct of_device_id sunxi_rsb_of_match_table[] = {
  664. { .compatible = "allwinner,sun8i-a23-rsb" },
  665. {}
  666. };
  667. MODULE_DEVICE_TABLE(of, sunxi_rsb_of_match_table);
  668. static struct platform_driver sunxi_rsb_driver = {
  669. .probe = sunxi_rsb_probe,
  670. .remove = sunxi_rsb_remove,
  671. .driver = {
  672. .name = RSB_CTRL_NAME,
  673. .of_match_table = sunxi_rsb_of_match_table,
  674. .pm = &sunxi_rsb_dev_pm_ops,
  675. },
  676. };
  677. static int __init sunxi_rsb_init(void)
  678. {
  679. int ret;
  680. ret = bus_register(&sunxi_rsb_bus);
  681. if (ret) {
  682. pr_err("failed to register sunxi sunxi_rsb bus: %d\n", ret);
  683. return ret;
  684. }
  685. ret = platform_driver_register(&sunxi_rsb_driver);
  686. if (ret) {
  687. bus_unregister(&sunxi_rsb_bus);
  688. return ret;
  689. }
  690. return 0;
  691. }
  692. module_init(sunxi_rsb_init);
  693. static void __exit sunxi_rsb_exit(void)
  694. {
  695. platform_driver_unregister(&sunxi_rsb_driver);
  696. bus_unregister(&sunxi_rsb_bus);
  697. }
  698. module_exit(sunxi_rsb_exit);
  699. MODULE_AUTHOR("Chen-Yu Tsai <wens@csie.org>");
  700. MODULE_DESCRIPTION("Allwinner sunXi Reduced Serial Bus controller driver");
  701. MODULE_LICENSE("GPL v2");