sunxi-rsb.c 19 KB

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