pinctrl-ti-iodelay.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  1. /*
  2. * Support for configuration of IO Delay module found on Texas Instruments SoCs
  3. * such as DRA7
  4. *
  5. * Copyright (C) 2015-2017 Texas Instruments Incorporated - https://www.ti.com/
  6. *
  7. * This file is licensed under the terms of the GNU General Public
  8. * License version 2. This program is licensed "as is" without any
  9. * warranty of any kind, whether express or implied.
  10. */
  11. #include <linux/err.h>
  12. #include <linux/init.h>
  13. #include <linux/io.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/of_device.h>
  17. #include <linux/pinctrl/pinconf.h>
  18. #include <linux/pinctrl/pinconf-generic.h>
  19. #include <linux/pinctrl/pinctrl.h>
  20. #include <linux/regmap.h>
  21. #include <linux/slab.h>
  22. #include "../core.h"
  23. #include "../devicetree.h"
  24. #define DRIVER_NAME "ti-iodelay"
  25. /**
  26. * struct ti_iodelay_reg_data - Describes the registers for the iodelay instance
  27. * @signature_mask: CONFIG_REG mask for the signature bits (see TRM)
  28. * @signature_value: CONFIG_REG signature value to be written (see TRM)
  29. * @lock_mask: CONFIG_REG mask for the lock bits (see TRM)
  30. * @lock_val: CONFIG_REG lock value for the lock bits (see TRM)
  31. * @unlock_val:CONFIG_REG unlock value for the lock bits (see TRM)
  32. * @binary_data_coarse_mask: CONFIG_REG coarse mask (see TRM)
  33. * @binary_data_fine_mask: CONFIG_REG fine mask (see TRM)
  34. * @reg_refclk_offset: Refclk register offset
  35. * @refclk_period_mask: Refclk mask
  36. * @reg_coarse_offset: Coarse register configuration offset
  37. * @coarse_delay_count_mask: Coarse delay count mask
  38. * @coarse_ref_count_mask: Coarse ref count mask
  39. * @reg_fine_offset: Fine register configuration offset
  40. * @fine_delay_count_mask: Fine delay count mask
  41. * @fine_ref_count_mask: Fine ref count mask
  42. * @reg_global_lock_offset: Global iodelay module lock register offset
  43. * @global_lock_mask: Lock mask
  44. * @global_unlock_val: Unlock value
  45. * @global_lock_val: Lock value
  46. * @reg_start_offset: Offset to iodelay registers after the CONFIG_REG_0 to 8
  47. * @reg_nr_per_pin: Number of iodelay registers for each pin
  48. * @regmap_config: Regmap configuration for the IODelay region
  49. */
  50. struct ti_iodelay_reg_data {
  51. u32 signature_mask;
  52. u32 signature_value;
  53. u32 lock_mask;
  54. u32 lock_val;
  55. u32 unlock_val;
  56. u32 binary_data_coarse_mask;
  57. u32 binary_data_fine_mask;
  58. u32 reg_refclk_offset;
  59. u32 refclk_period_mask;
  60. u32 reg_coarse_offset;
  61. u32 coarse_delay_count_mask;
  62. u32 coarse_ref_count_mask;
  63. u32 reg_fine_offset;
  64. u32 fine_delay_count_mask;
  65. u32 fine_ref_count_mask;
  66. u32 reg_global_lock_offset;
  67. u32 global_lock_mask;
  68. u32 global_unlock_val;
  69. u32 global_lock_val;
  70. u32 reg_start_offset;
  71. u32 reg_nr_per_pin;
  72. struct regmap_config *regmap_config;
  73. };
  74. /**
  75. * struct ti_iodelay_reg_values - Computed io_reg configuration values (see TRM)
  76. * @coarse_ref_count: Coarse reference count
  77. * @coarse_delay_count: Coarse delay count
  78. * @fine_ref_count: Fine reference count
  79. * @fine_delay_count: Fine Delay count
  80. * @ref_clk_period: Reference Clock period
  81. * @cdpe: Coarse delay parameter
  82. * @fdpe: Fine delay parameter
  83. */
  84. struct ti_iodelay_reg_values {
  85. u16 coarse_ref_count;
  86. u16 coarse_delay_count;
  87. u16 fine_ref_count;
  88. u16 fine_delay_count;
  89. u16 ref_clk_period;
  90. u32 cdpe;
  91. u32 fdpe;
  92. };
  93. /**
  94. * struct ti_iodelay_cfg - Description of each configuration parameters
  95. * @offset: Configuration register offset
  96. * @a_delay: Agnostic Delay (in ps)
  97. * @g_delay: Gnostic Delay (in ps)
  98. */
  99. struct ti_iodelay_cfg {
  100. u16 offset;
  101. u16 a_delay;
  102. u16 g_delay;
  103. };
  104. /**
  105. * struct ti_iodelay_pingroup - Structure that describes one group
  106. * @cfg: configuration array for the pin (from dt)
  107. * @ncfg: number of configuration values allocated
  108. * @config: pinconf "Config" - currently a dummy value
  109. */
  110. struct ti_iodelay_pingroup {
  111. struct ti_iodelay_cfg *cfg;
  112. int ncfg;
  113. unsigned long config;
  114. };
  115. /**
  116. * struct ti_iodelay_device - Represents information for a iodelay instance
  117. * @dev: Device pointer
  118. * @phys_base: Physical address base of the iodelay device
  119. * @reg_base: Virtual address base of the iodelay device
  120. * @regmap: Regmap for this iodelay instance
  121. * @pctl: Pinctrl device
  122. * @desc: pinctrl descriptor for pctl
  123. * @pa: pinctrl pin wise description
  124. * @reg_data: Register definition data for the IODelay instance
  125. * @reg_init_conf_values: Initial configuration values.
  126. */
  127. struct ti_iodelay_device {
  128. struct device *dev;
  129. unsigned long phys_base;
  130. void __iomem *reg_base;
  131. struct regmap *regmap;
  132. struct pinctrl_dev *pctl;
  133. struct pinctrl_desc desc;
  134. struct pinctrl_pin_desc *pa;
  135. const struct ti_iodelay_reg_data *reg_data;
  136. struct ti_iodelay_reg_values reg_init_conf_values;
  137. };
  138. /**
  139. * ti_iodelay_extract() - extract bits for a field
  140. * @val: Register value
  141. * @mask: Mask
  142. *
  143. * Return: extracted value which is appropriately shifted
  144. */
  145. static inline u32 ti_iodelay_extract(u32 val, u32 mask)
  146. {
  147. return (val & mask) >> __ffs(mask);
  148. }
  149. /**
  150. * ti_iodelay_compute_dpe() - Compute equation for delay parameter
  151. * @period: Period to use
  152. * @ref: Reference Count
  153. * @delay: Delay count
  154. * @delay_m: Delay multiplier
  155. *
  156. * Return: Computed delay parameter
  157. */
  158. static inline u32 ti_iodelay_compute_dpe(u16 period, u16 ref, u16 delay,
  159. u16 delay_m)
  160. {
  161. u64 m, d;
  162. /* Handle overflow conditions */
  163. m = 10 * (u64)period * (u64)ref;
  164. d = 2 * (u64)delay * (u64)delay_m;
  165. /* Truncate result back to 32 bits */
  166. return div64_u64(m, d);
  167. }
  168. /**
  169. * ti_iodelay_pinconf_set() - Configure the pin configuration
  170. * @iod: iodelay device
  171. * @cfg: Configuration
  172. *
  173. * Update the configuration register as per TRM and lockup once done.
  174. * *IMPORTANT NOTE* SoC TRM does recommend doing iodelay programmation only
  175. * while in Isolation. But, then, isolation also implies that every pin
  176. * on the SoC (including DDR) will be isolated out. The only benefit being
  177. * a glitchless configuration, However, the intent of this driver is purely
  178. * to support a "glitchy" configuration where applicable.
  179. *
  180. * Return: 0 in case of success, else appropriate error value
  181. */
  182. static int ti_iodelay_pinconf_set(struct ti_iodelay_device *iod,
  183. struct ti_iodelay_cfg *cfg)
  184. {
  185. const struct ti_iodelay_reg_data *reg = iod->reg_data;
  186. struct ti_iodelay_reg_values *ival = &iod->reg_init_conf_values;
  187. struct device *dev = iod->dev;
  188. u32 g_delay_coarse, g_delay_fine;
  189. u32 a_delay_coarse, a_delay_fine;
  190. u32 c_elements, f_elements;
  191. u32 total_delay;
  192. u32 reg_mask, reg_val, tmp_val;
  193. int r;
  194. /* NOTE: Truncation is expected in all division below */
  195. g_delay_coarse = cfg->g_delay / 920;
  196. g_delay_fine = ((cfg->g_delay % 920) * 10) / 60;
  197. a_delay_coarse = cfg->a_delay / ival->cdpe;
  198. a_delay_fine = ((cfg->a_delay % ival->cdpe) * 10) / ival->fdpe;
  199. c_elements = g_delay_coarse + a_delay_coarse;
  200. f_elements = (g_delay_fine + a_delay_fine) / 10;
  201. if (f_elements > 22) {
  202. total_delay = c_elements * ival->cdpe + f_elements * ival->fdpe;
  203. c_elements = total_delay / ival->cdpe;
  204. f_elements = (total_delay % ival->cdpe) / ival->fdpe;
  205. }
  206. reg_mask = reg->signature_mask;
  207. reg_val = reg->signature_value << __ffs(reg->signature_mask);
  208. reg_mask |= reg->binary_data_coarse_mask;
  209. tmp_val = c_elements << __ffs(reg->binary_data_coarse_mask);
  210. if (tmp_val & ~reg->binary_data_coarse_mask) {
  211. dev_err(dev, "Masking overflow of coarse elements %08x\n",
  212. tmp_val);
  213. tmp_val &= reg->binary_data_coarse_mask;
  214. }
  215. reg_val |= tmp_val;
  216. reg_mask |= reg->binary_data_fine_mask;
  217. tmp_val = f_elements << __ffs(reg->binary_data_fine_mask);
  218. if (tmp_val & ~reg->binary_data_fine_mask) {
  219. dev_err(dev, "Masking overflow of fine elements %08x\n",
  220. tmp_val);
  221. tmp_val &= reg->binary_data_fine_mask;
  222. }
  223. reg_val |= tmp_val;
  224. /*
  225. * NOTE: we leave the iodelay values unlocked - this is to work around
  226. * situations such as those found with mmc mode change.
  227. * However, this leaves open any unwarranted changes to padconf register
  228. * impacting iodelay configuration. Use with care!
  229. */
  230. reg_mask |= reg->lock_mask;
  231. reg_val |= reg->unlock_val << __ffs(reg->lock_mask);
  232. r = regmap_update_bits(iod->regmap, cfg->offset, reg_mask, reg_val);
  233. dev_dbg(dev, "Set reg 0x%x Delay(a: %d g: %d), Elements(C=%d F=%d)0x%x\n",
  234. cfg->offset, cfg->a_delay, cfg->g_delay, c_elements,
  235. f_elements, reg_val);
  236. return r;
  237. }
  238. /**
  239. * ti_iodelay_pinconf_init_dev() - Initialize IODelay device
  240. * @iod: iodelay device
  241. *
  242. * Unlocks the iodelay region, computes the common parameters
  243. *
  244. * Return: 0 in case of success, else appropriate error value
  245. */
  246. static int ti_iodelay_pinconf_init_dev(struct ti_iodelay_device *iod)
  247. {
  248. const struct ti_iodelay_reg_data *reg = iod->reg_data;
  249. struct device *dev = iod->dev;
  250. struct ti_iodelay_reg_values *ival = &iod->reg_init_conf_values;
  251. u32 val;
  252. int r;
  253. /* unlock the iodelay region */
  254. r = regmap_update_bits(iod->regmap, reg->reg_global_lock_offset,
  255. reg->global_lock_mask, reg->global_unlock_val);
  256. if (r)
  257. return r;
  258. /* Read up Recalibration sequence done by bootloader */
  259. r = regmap_read(iod->regmap, reg->reg_refclk_offset, &val);
  260. if (r)
  261. return r;
  262. ival->ref_clk_period = ti_iodelay_extract(val, reg->refclk_period_mask);
  263. dev_dbg(dev, "refclk_period=0x%04x\n", ival->ref_clk_period);
  264. r = regmap_read(iod->regmap, reg->reg_coarse_offset, &val);
  265. if (r)
  266. return r;
  267. ival->coarse_ref_count =
  268. ti_iodelay_extract(val, reg->coarse_ref_count_mask);
  269. ival->coarse_delay_count =
  270. ti_iodelay_extract(val, reg->coarse_delay_count_mask);
  271. if (!ival->coarse_delay_count) {
  272. dev_err(dev, "Invalid Coarse delay count (0) (reg=0x%08x)\n",
  273. val);
  274. return -EINVAL;
  275. }
  276. ival->cdpe = ti_iodelay_compute_dpe(ival->ref_clk_period,
  277. ival->coarse_ref_count,
  278. ival->coarse_delay_count, 88);
  279. if (!ival->cdpe) {
  280. dev_err(dev, "Invalid cdpe computed params = %d %d %d\n",
  281. ival->ref_clk_period, ival->coarse_ref_count,
  282. ival->coarse_delay_count);
  283. return -EINVAL;
  284. }
  285. dev_dbg(iod->dev, "coarse: ref=0x%04x delay=0x%04x cdpe=0x%08x\n",
  286. ival->coarse_ref_count, ival->coarse_delay_count, ival->cdpe);
  287. r = regmap_read(iod->regmap, reg->reg_fine_offset, &val);
  288. if (r)
  289. return r;
  290. ival->fine_ref_count =
  291. ti_iodelay_extract(val, reg->fine_ref_count_mask);
  292. ival->fine_delay_count =
  293. ti_iodelay_extract(val, reg->fine_delay_count_mask);
  294. if (!ival->fine_delay_count) {
  295. dev_err(dev, "Invalid Fine delay count (0) (reg=0x%08x)\n",
  296. val);
  297. return -EINVAL;
  298. }
  299. ival->fdpe = ti_iodelay_compute_dpe(ival->ref_clk_period,
  300. ival->fine_ref_count,
  301. ival->fine_delay_count, 264);
  302. if (!ival->fdpe) {
  303. dev_err(dev, "Invalid fdpe(0) computed params = %d %d %d\n",
  304. ival->ref_clk_period, ival->fine_ref_count,
  305. ival->fine_delay_count);
  306. return -EINVAL;
  307. }
  308. dev_dbg(iod->dev, "fine: ref=0x%04x delay=0x%04x fdpe=0x%08x\n",
  309. ival->fine_ref_count, ival->fine_delay_count, ival->fdpe);
  310. return 0;
  311. }
  312. /**
  313. * ti_iodelay_pinconf_deinit_dev() - deinit the iodelay device
  314. * @iod: IODelay device
  315. *
  316. * Deinitialize the IODelay device (basically just lock the region back up.
  317. */
  318. static void ti_iodelay_pinconf_deinit_dev(struct ti_iodelay_device *iod)
  319. {
  320. const struct ti_iodelay_reg_data *reg = iod->reg_data;
  321. /* lock the iodelay region back again */
  322. regmap_update_bits(iod->regmap, reg->reg_global_lock_offset,
  323. reg->global_lock_mask, reg->global_lock_val);
  324. }
  325. /**
  326. * ti_iodelay_get_pingroup() - Find the group mapped by a group selector
  327. * @iod: iodelay device
  328. * @selector: Group Selector
  329. *
  330. * Return: Corresponding group representing group selector
  331. */
  332. static struct ti_iodelay_pingroup *
  333. ti_iodelay_get_pingroup(struct ti_iodelay_device *iod, unsigned int selector)
  334. {
  335. struct group_desc *g;
  336. g = pinctrl_generic_get_group(iod->pctl, selector);
  337. if (!g) {
  338. dev_err(iod->dev, "%s could not find pingroup %i\n", __func__,
  339. selector);
  340. return NULL;
  341. }
  342. return g->data;
  343. }
  344. /**
  345. * ti_iodelay_offset_to_pin() - get a pin index based on the register offset
  346. * @iod: iodelay driver instance
  347. * @offset: register offset from the base
  348. */
  349. static int ti_iodelay_offset_to_pin(struct ti_iodelay_device *iod,
  350. unsigned int offset)
  351. {
  352. const struct ti_iodelay_reg_data *r = iod->reg_data;
  353. unsigned int index;
  354. if (offset > r->regmap_config->max_register) {
  355. dev_err(iod->dev, "mux offset out of range: 0x%x (0x%x)\n",
  356. offset, r->regmap_config->max_register);
  357. return -EINVAL;
  358. }
  359. index = (offset - r->reg_start_offset) / r->regmap_config->reg_stride;
  360. index /= r->reg_nr_per_pin;
  361. return index;
  362. }
  363. /**
  364. * ti_iodelay_node_iterator() - Iterate iodelay node
  365. * @pctldev: Pin controller driver
  366. * @np: Device node
  367. * @pinctrl_spec: Parsed arguments from device tree
  368. * @pins: Array of pins in the pin group
  369. * @pin_index: Pin index in the pin array
  370. * @data: Pin controller driver specific data
  371. *
  372. */
  373. static int ti_iodelay_node_iterator(struct pinctrl_dev *pctldev,
  374. struct device_node *np,
  375. const struct of_phandle_args *pinctrl_spec,
  376. int *pins, int pin_index, void *data)
  377. {
  378. struct ti_iodelay_device *iod;
  379. struct ti_iodelay_cfg *cfg = data;
  380. const struct ti_iodelay_reg_data *r;
  381. struct pinctrl_pin_desc *pd;
  382. int pin;
  383. iod = pinctrl_dev_get_drvdata(pctldev);
  384. if (!iod)
  385. return -EINVAL;
  386. r = iod->reg_data;
  387. if (pinctrl_spec->args_count < r->reg_nr_per_pin) {
  388. dev_err(iod->dev, "invalid args_count for spec: %i\n",
  389. pinctrl_spec->args_count);
  390. return -EINVAL;
  391. }
  392. /* Index plus two value cells */
  393. cfg[pin_index].offset = pinctrl_spec->args[0];
  394. cfg[pin_index].a_delay = pinctrl_spec->args[1] & 0xffff;
  395. cfg[pin_index].g_delay = pinctrl_spec->args[2] & 0xffff;
  396. pin = ti_iodelay_offset_to_pin(iod, cfg[pin_index].offset);
  397. if (pin < 0) {
  398. dev_err(iod->dev, "could not add functions for %pOFn %ux\n",
  399. np, cfg[pin_index].offset);
  400. return -ENODEV;
  401. }
  402. pins[pin_index] = pin;
  403. pd = &iod->pa[pin];
  404. pd->drv_data = &cfg[pin_index];
  405. dev_dbg(iod->dev, "%pOFn offset=%x a_delay = %d g_delay = %d\n",
  406. np, cfg[pin_index].offset, cfg[pin_index].a_delay,
  407. cfg[pin_index].g_delay);
  408. return 0;
  409. }
  410. /**
  411. * ti_iodelay_dt_node_to_map() - Map a device tree node to appropriate group
  412. * @pctldev: pinctrl device representing IODelay device
  413. * @np: Node Pointer (device tree)
  414. * @map: Pinctrl Map returned back to pinctrl framework
  415. * @num_maps: Number of maps (1)
  416. *
  417. * Maps the device tree description into a group of configuration parameters
  418. * for iodelay block entry.
  419. *
  420. * Return: 0 in case of success, else appropriate error value
  421. */
  422. static int ti_iodelay_dt_node_to_map(struct pinctrl_dev *pctldev,
  423. struct device_node *np,
  424. struct pinctrl_map **map,
  425. unsigned int *num_maps)
  426. {
  427. struct ti_iodelay_device *iod;
  428. struct ti_iodelay_cfg *cfg;
  429. struct ti_iodelay_pingroup *g;
  430. const char *name = "pinctrl-pin-array";
  431. int rows, *pins, error = -EINVAL, i;
  432. iod = pinctrl_dev_get_drvdata(pctldev);
  433. if (!iod)
  434. return -EINVAL;
  435. rows = pinctrl_count_index_with_args(np, name);
  436. if (rows < 0)
  437. return rows;
  438. *map = devm_kzalloc(iod->dev, sizeof(**map), GFP_KERNEL);
  439. if (!*map)
  440. return -ENOMEM;
  441. *num_maps = 0;
  442. g = devm_kzalloc(iod->dev, sizeof(*g), GFP_KERNEL);
  443. if (!g) {
  444. error = -ENOMEM;
  445. goto free_map;
  446. }
  447. pins = devm_kcalloc(iod->dev, rows, sizeof(*pins), GFP_KERNEL);
  448. if (!pins) {
  449. error = -ENOMEM;
  450. goto free_group;
  451. }
  452. cfg = devm_kcalloc(iod->dev, rows, sizeof(*cfg), GFP_KERNEL);
  453. if (!cfg) {
  454. error = -ENOMEM;
  455. goto free_pins;
  456. }
  457. for (i = 0; i < rows; i++) {
  458. struct of_phandle_args pinctrl_spec;
  459. error = pinctrl_parse_index_with_args(np, name, i,
  460. &pinctrl_spec);
  461. if (error)
  462. goto free_data;
  463. error = ti_iodelay_node_iterator(pctldev, np, &pinctrl_spec,
  464. pins, i, cfg);
  465. if (error)
  466. goto free_data;
  467. }
  468. g->cfg = cfg;
  469. g->ncfg = i;
  470. g->config = PIN_CONFIG_END;
  471. error = pinctrl_generic_add_group(iod->pctl, np->name, pins, i, g);
  472. if (error < 0)
  473. goto free_data;
  474. (*map)->type = PIN_MAP_TYPE_CONFIGS_GROUP;
  475. (*map)->data.configs.group_or_pin = np->name;
  476. (*map)->data.configs.configs = &g->config;
  477. (*map)->data.configs.num_configs = 1;
  478. *num_maps = 1;
  479. return 0;
  480. free_data:
  481. devm_kfree(iod->dev, cfg);
  482. free_pins:
  483. devm_kfree(iod->dev, pins);
  484. free_group:
  485. devm_kfree(iod->dev, g);
  486. free_map:
  487. devm_kfree(iod->dev, *map);
  488. return error;
  489. }
  490. /**
  491. * ti_iodelay_pinconf_group_get() - Get the group configuration
  492. * @pctldev: pinctrl device representing IODelay device
  493. * @selector: Group selector
  494. * @config: Configuration returned
  495. *
  496. * Return: The configuration if the group is valid, else returns -EINVAL
  497. */
  498. static int ti_iodelay_pinconf_group_get(struct pinctrl_dev *pctldev,
  499. unsigned int selector,
  500. unsigned long *config)
  501. {
  502. struct ti_iodelay_device *iod;
  503. struct ti_iodelay_pingroup *group;
  504. iod = pinctrl_dev_get_drvdata(pctldev);
  505. group = ti_iodelay_get_pingroup(iod, selector);
  506. if (!group)
  507. return -EINVAL;
  508. *config = group->config;
  509. return 0;
  510. }
  511. /**
  512. * ti_iodelay_pinconf_group_set() - Configure the groups of pins
  513. * @pctldev: pinctrl device representing IODelay device
  514. * @selector: Group selector
  515. * @configs: Configurations
  516. * @num_configs: Number of configurations
  517. *
  518. * Return: 0 if all went fine, else appropriate error value.
  519. */
  520. static int ti_iodelay_pinconf_group_set(struct pinctrl_dev *pctldev,
  521. unsigned int selector,
  522. unsigned long *configs,
  523. unsigned int num_configs)
  524. {
  525. struct ti_iodelay_device *iod;
  526. struct device *dev;
  527. struct ti_iodelay_pingroup *group;
  528. int i;
  529. iod = pinctrl_dev_get_drvdata(pctldev);
  530. dev = iod->dev;
  531. group = ti_iodelay_get_pingroup(iod, selector);
  532. if (num_configs != 1) {
  533. dev_err(dev, "Unsupported number of configurations %d\n",
  534. num_configs);
  535. return -EINVAL;
  536. }
  537. if (*configs != PIN_CONFIG_END) {
  538. dev_err(dev, "Unsupported configuration\n");
  539. return -EINVAL;
  540. }
  541. for (i = 0; i < group->ncfg; i++) {
  542. if (ti_iodelay_pinconf_set(iod, &group->cfg[i]))
  543. return -ENOTSUPP;
  544. }
  545. return 0;
  546. }
  547. #ifdef CONFIG_DEBUG_FS
  548. /**
  549. * ti_iodelay_pin_to_offset() - get pin register offset based on the pin index
  550. * @iod: iodelay driver instance
  551. * @selector: Pin index
  552. */
  553. static unsigned int ti_iodelay_pin_to_offset(struct ti_iodelay_device *iod,
  554. unsigned int selector)
  555. {
  556. const struct ti_iodelay_reg_data *r = iod->reg_data;
  557. unsigned int offset;
  558. offset = selector * r->regmap_config->reg_stride;
  559. offset *= r->reg_nr_per_pin;
  560. offset += r->reg_start_offset;
  561. return offset;
  562. }
  563. static void ti_iodelay_pin_dbg_show(struct pinctrl_dev *pctldev,
  564. struct seq_file *s,
  565. unsigned int pin)
  566. {
  567. struct ti_iodelay_device *iod;
  568. struct pinctrl_pin_desc *pd;
  569. struct ti_iodelay_cfg *cfg;
  570. const struct ti_iodelay_reg_data *r;
  571. unsigned long offset;
  572. u32 in, oen, out;
  573. iod = pinctrl_dev_get_drvdata(pctldev);
  574. r = iod->reg_data;
  575. offset = ti_iodelay_pin_to_offset(iod, pin);
  576. pd = &iod->pa[pin];
  577. cfg = pd->drv_data;
  578. regmap_read(iod->regmap, offset, &in);
  579. regmap_read(iod->regmap, offset + r->regmap_config->reg_stride, &oen);
  580. regmap_read(iod->regmap, offset + r->regmap_config->reg_stride * 2,
  581. &out);
  582. seq_printf(s, "%lx a: %i g: %i (%08x %08x %08x) %s ",
  583. iod->phys_base + offset,
  584. cfg ? cfg->a_delay : -1,
  585. cfg ? cfg->g_delay : -1,
  586. in, oen, out, DRIVER_NAME);
  587. }
  588. /**
  589. * ti_iodelay_pinconf_group_dbg_show() - show the group information
  590. * @pctldev: Show the group information
  591. * @s: Sequence file
  592. * @selector: Group selector
  593. *
  594. * Provide the configuration information of the selected group
  595. */
  596. static void ti_iodelay_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
  597. struct seq_file *s,
  598. unsigned int selector)
  599. {
  600. struct ti_iodelay_device *iod;
  601. struct ti_iodelay_pingroup *group;
  602. int i;
  603. iod = pinctrl_dev_get_drvdata(pctldev);
  604. group = ti_iodelay_get_pingroup(iod, selector);
  605. if (!group)
  606. return;
  607. for (i = 0; i < group->ncfg; i++) {
  608. struct ti_iodelay_cfg *cfg;
  609. u32 reg = 0;
  610. cfg = &group->cfg[i];
  611. regmap_read(iod->regmap, cfg->offset, &reg);
  612. seq_printf(s, "\n\t0x%08x = 0x%08x (%3d, %3d)",
  613. cfg->offset, reg, cfg->a_delay, cfg->g_delay);
  614. }
  615. }
  616. #endif
  617. static const struct pinctrl_ops ti_iodelay_pinctrl_ops = {
  618. .get_groups_count = pinctrl_generic_get_group_count,
  619. .get_group_name = pinctrl_generic_get_group_name,
  620. .get_group_pins = pinctrl_generic_get_group_pins,
  621. #ifdef CONFIG_DEBUG_FS
  622. .pin_dbg_show = ti_iodelay_pin_dbg_show,
  623. #endif
  624. .dt_node_to_map = ti_iodelay_dt_node_to_map,
  625. };
  626. static const struct pinconf_ops ti_iodelay_pinctrl_pinconf_ops = {
  627. .pin_config_group_get = ti_iodelay_pinconf_group_get,
  628. .pin_config_group_set = ti_iodelay_pinconf_group_set,
  629. #ifdef CONFIG_DEBUG_FS
  630. .pin_config_group_dbg_show = ti_iodelay_pinconf_group_dbg_show,
  631. #endif
  632. };
  633. /**
  634. * ti_iodelay_alloc_pins() - Allocate structures needed for pins for iodelay
  635. * @dev: Device pointer
  636. * @iod: iodelay device
  637. * @base_phy: Base Physical Address
  638. *
  639. * Return: 0 if all went fine, else appropriate error value.
  640. */
  641. static int ti_iodelay_alloc_pins(struct device *dev,
  642. struct ti_iodelay_device *iod, u32 base_phy)
  643. {
  644. const struct ti_iodelay_reg_data *r = iod->reg_data;
  645. struct pinctrl_pin_desc *pin;
  646. u32 phy_reg;
  647. int nr_pins, i;
  648. nr_pins = ti_iodelay_offset_to_pin(iod, r->regmap_config->max_register);
  649. dev_dbg(dev, "Allocating %i pins\n", nr_pins);
  650. iod->pa = devm_kcalloc(dev, nr_pins, sizeof(*iod->pa), GFP_KERNEL);
  651. if (!iod->pa)
  652. return -ENOMEM;
  653. iod->desc.pins = iod->pa;
  654. iod->desc.npins = nr_pins;
  655. phy_reg = r->reg_start_offset + base_phy;
  656. for (i = 0; i < nr_pins; i++, phy_reg += 4) {
  657. pin = &iod->pa[i];
  658. pin->number = i;
  659. }
  660. return 0;
  661. }
  662. static struct regmap_config dra7_iodelay_regmap_config = {
  663. .reg_bits = 32,
  664. .reg_stride = 4,
  665. .val_bits = 32,
  666. .max_register = 0xd1c,
  667. };
  668. static struct ti_iodelay_reg_data dra7_iodelay_data = {
  669. .signature_mask = 0x0003f000,
  670. .signature_value = 0x29,
  671. .lock_mask = 0x00000400,
  672. .lock_val = 1,
  673. .unlock_val = 0,
  674. .binary_data_coarse_mask = 0x000003e0,
  675. .binary_data_fine_mask = 0x0000001f,
  676. .reg_refclk_offset = 0x14,
  677. .refclk_period_mask = 0xffff,
  678. .reg_coarse_offset = 0x18,
  679. .coarse_delay_count_mask = 0xffff0000,
  680. .coarse_ref_count_mask = 0x0000ffff,
  681. .reg_fine_offset = 0x1C,
  682. .fine_delay_count_mask = 0xffff0000,
  683. .fine_ref_count_mask = 0x0000ffff,
  684. .reg_global_lock_offset = 0x2c,
  685. .global_lock_mask = 0x0000ffff,
  686. .global_unlock_val = 0x0000aaaa,
  687. .global_lock_val = 0x0000aaab,
  688. .reg_start_offset = 0x30,
  689. .reg_nr_per_pin = 3,
  690. .regmap_config = &dra7_iodelay_regmap_config,
  691. };
  692. static const struct of_device_id ti_iodelay_of_match[] = {
  693. {.compatible = "ti,dra7-iodelay", .data = &dra7_iodelay_data},
  694. { /* Hopefully no more.. */ },
  695. };
  696. MODULE_DEVICE_TABLE(of, ti_iodelay_of_match);
  697. /**
  698. * ti_iodelay_probe() - Standard probe
  699. * @pdev: platform device
  700. *
  701. * Return: 0 if all went fine, else appropriate error value.
  702. */
  703. static int ti_iodelay_probe(struct platform_device *pdev)
  704. {
  705. struct device *dev = &pdev->dev;
  706. struct device_node *np = of_node_get(dev->of_node);
  707. const struct of_device_id *match;
  708. struct resource *res;
  709. struct ti_iodelay_device *iod;
  710. int ret = 0;
  711. if (!np) {
  712. ret = -EINVAL;
  713. dev_err(dev, "No OF node\n");
  714. goto exit_out;
  715. }
  716. match = of_match_device(ti_iodelay_of_match, dev);
  717. if (!match) {
  718. ret = -EINVAL;
  719. dev_err(dev, "No DATA match\n");
  720. goto exit_out;
  721. }
  722. iod = devm_kzalloc(dev, sizeof(*iod), GFP_KERNEL);
  723. if (!iod) {
  724. ret = -ENOMEM;
  725. goto exit_out;
  726. }
  727. iod->dev = dev;
  728. iod->reg_data = match->data;
  729. /* So far We can assume there is only 1 bank of registers */
  730. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  731. if (!res) {
  732. dev_err(dev, "Missing MEM resource\n");
  733. ret = -ENODEV;
  734. goto exit_out;
  735. }
  736. iod->phys_base = res->start;
  737. iod->reg_base = devm_ioremap_resource(dev, res);
  738. if (IS_ERR(iod->reg_base)) {
  739. ret = PTR_ERR(iod->reg_base);
  740. goto exit_out;
  741. }
  742. iod->regmap = devm_regmap_init_mmio(dev, iod->reg_base,
  743. iod->reg_data->regmap_config);
  744. if (IS_ERR(iod->regmap)) {
  745. dev_err(dev, "Regmap MMIO init failed.\n");
  746. ret = PTR_ERR(iod->regmap);
  747. goto exit_out;
  748. }
  749. ret = ti_iodelay_pinconf_init_dev(iod);
  750. if (ret)
  751. goto exit_out;
  752. ret = ti_iodelay_alloc_pins(dev, iod, res->start);
  753. if (ret)
  754. goto exit_out;
  755. iod->desc.pctlops = &ti_iodelay_pinctrl_ops;
  756. /* no pinmux ops - we are pinconf */
  757. iod->desc.confops = &ti_iodelay_pinctrl_pinconf_ops;
  758. iod->desc.name = dev_name(dev);
  759. iod->desc.owner = THIS_MODULE;
  760. ret = pinctrl_register_and_init(&iod->desc, dev, iod, &iod->pctl);
  761. if (ret) {
  762. dev_err(dev, "Failed to register pinctrl\n");
  763. goto exit_out;
  764. }
  765. platform_set_drvdata(pdev, iod);
  766. return pinctrl_enable(iod->pctl);
  767. exit_out:
  768. of_node_put(np);
  769. return ret;
  770. }
  771. /**
  772. * ti_iodelay_remove() - standard remove
  773. * @pdev: platform device
  774. *
  775. * Return: 0 if all went fine, else appropriate error value.
  776. */
  777. static int ti_iodelay_remove(struct platform_device *pdev)
  778. {
  779. struct ti_iodelay_device *iod = platform_get_drvdata(pdev);
  780. if (!iod)
  781. return 0;
  782. if (iod->pctl)
  783. pinctrl_unregister(iod->pctl);
  784. ti_iodelay_pinconf_deinit_dev(iod);
  785. /* Expect other allocations to be freed by devm */
  786. return 0;
  787. }
  788. static struct platform_driver ti_iodelay_driver = {
  789. .probe = ti_iodelay_probe,
  790. .remove = ti_iodelay_remove,
  791. .driver = {
  792. .name = DRIVER_NAME,
  793. .of_match_table = ti_iodelay_of_match,
  794. },
  795. };
  796. module_platform_driver(ti_iodelay_driver);
  797. MODULE_AUTHOR("Texas Instruments, Inc.");
  798. MODULE_DESCRIPTION("Pinconf driver for TI's IO Delay module");
  799. MODULE_LICENSE("GPL v2");