pinctrl-ti-iodelay.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  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 - http://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_info(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 %s %ux\n",
  399. np->name, 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, "%s offset=%x a_delay = %d g_delay = %d\n",
  406. np->name, 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. goto free_group;
  450. cfg = devm_kcalloc(iod->dev, rows, sizeof(*cfg), GFP_KERNEL);
  451. if (!cfg) {
  452. error = -ENOMEM;
  453. goto free_pins;
  454. }
  455. for (i = 0; i < rows; i++) {
  456. struct of_phandle_args pinctrl_spec;
  457. error = pinctrl_parse_index_with_args(np, name, i,
  458. &pinctrl_spec);
  459. if (error)
  460. goto free_data;
  461. error = ti_iodelay_node_iterator(pctldev, np, &pinctrl_spec,
  462. pins, i, cfg);
  463. if (error)
  464. goto free_data;
  465. }
  466. g->cfg = cfg;
  467. g->ncfg = i;
  468. g->config = PIN_CONFIG_END;
  469. error = pinctrl_generic_add_group(iod->pctl, np->name, pins, i, g);
  470. if (error < 0)
  471. goto free_data;
  472. (*map)->type = PIN_MAP_TYPE_CONFIGS_GROUP;
  473. (*map)->data.configs.group_or_pin = np->name;
  474. (*map)->data.configs.configs = &g->config;
  475. (*map)->data.configs.num_configs = 1;
  476. *num_maps = 1;
  477. return 0;
  478. free_data:
  479. devm_kfree(iod->dev, cfg);
  480. free_pins:
  481. devm_kfree(iod->dev, pins);
  482. free_group:
  483. devm_kfree(iod->dev, g);
  484. free_map:
  485. devm_kfree(iod->dev, *map);
  486. return error;
  487. }
  488. /**
  489. * ti_iodelay_pinconf_group_get() - Get the group configuration
  490. * @pctldev: pinctrl device representing IODelay device
  491. * @selector: Group selector
  492. * @config: Configuration returned
  493. *
  494. * Return: The configuration if the group is valid, else returns -EINVAL
  495. */
  496. static int ti_iodelay_pinconf_group_get(struct pinctrl_dev *pctldev,
  497. unsigned int selector,
  498. unsigned long *config)
  499. {
  500. struct ti_iodelay_device *iod;
  501. struct ti_iodelay_pingroup *group;
  502. iod = pinctrl_dev_get_drvdata(pctldev);
  503. group = ti_iodelay_get_pingroup(iod, selector);
  504. if (!group)
  505. return -EINVAL;
  506. *config = group->config;
  507. return 0;
  508. }
  509. /**
  510. * ti_iodelay_pinconf_group_set() - Configure the groups of pins
  511. * @pctldev: pinctrl device representing IODelay device
  512. * @selector: Group selector
  513. * @configs: Configurations
  514. * @num_configs: Number of configurations
  515. *
  516. * Return: 0 if all went fine, else appropriate error value.
  517. */
  518. static int ti_iodelay_pinconf_group_set(struct pinctrl_dev *pctldev,
  519. unsigned int selector,
  520. unsigned long *configs,
  521. unsigned int num_configs)
  522. {
  523. struct ti_iodelay_device *iod;
  524. struct device *dev;
  525. struct ti_iodelay_pingroup *group;
  526. int i;
  527. iod = pinctrl_dev_get_drvdata(pctldev);
  528. dev = iod->dev;
  529. group = ti_iodelay_get_pingroup(iod, selector);
  530. if (num_configs != 1) {
  531. dev_err(dev, "Unsupported number of configurations %d\n",
  532. num_configs);
  533. return -EINVAL;
  534. }
  535. if (*configs != PIN_CONFIG_END) {
  536. dev_err(dev, "Unsupported configuration\n");
  537. return -EINVAL;
  538. }
  539. for (i = 0; i < group->ncfg; i++) {
  540. if (ti_iodelay_pinconf_set(iod, &group->cfg[i]))
  541. return -ENOTSUPP;
  542. }
  543. return 0;
  544. }
  545. #ifdef CONFIG_DEBUG_FS
  546. /**
  547. * ti_iodelay_pin_to_offset() - get pin register offset based on the pin index
  548. * @iod: iodelay driver instance
  549. * @selector: Pin index
  550. */
  551. static unsigned int ti_iodelay_pin_to_offset(struct ti_iodelay_device *iod,
  552. unsigned int selector)
  553. {
  554. const struct ti_iodelay_reg_data *r = iod->reg_data;
  555. unsigned int offset;
  556. offset = selector * r->regmap_config->reg_stride;
  557. offset *= r->reg_nr_per_pin;
  558. offset += r->reg_start_offset;
  559. return offset;
  560. }
  561. static void ti_iodelay_pin_dbg_show(struct pinctrl_dev *pctldev,
  562. struct seq_file *s,
  563. unsigned int pin)
  564. {
  565. struct ti_iodelay_device *iod;
  566. struct pinctrl_pin_desc *pd;
  567. struct ti_iodelay_cfg *cfg;
  568. const struct ti_iodelay_reg_data *r;
  569. unsigned long offset;
  570. u32 in, oen, out;
  571. iod = pinctrl_dev_get_drvdata(pctldev);
  572. r = iod->reg_data;
  573. offset = ti_iodelay_pin_to_offset(iod, pin);
  574. pd = &iod->pa[pin];
  575. cfg = pd->drv_data;
  576. regmap_read(iod->regmap, offset, &in);
  577. regmap_read(iod->regmap, offset + r->regmap_config->reg_stride, &oen);
  578. regmap_read(iod->regmap, offset + r->regmap_config->reg_stride * 2,
  579. &out);
  580. seq_printf(s, "%lx a: %i g: %i (%08x %08x %08x) %s ",
  581. iod->phys_base + offset,
  582. cfg ? cfg->a_delay : -1,
  583. cfg ? cfg->g_delay : -1,
  584. in, oen, out, DRIVER_NAME);
  585. }
  586. /**
  587. * ti_iodelay_pinconf_group_dbg_show() - show the group information
  588. * @pctldev: Show the group information
  589. * @s: Sequence file
  590. * @selector: Group selector
  591. *
  592. * Provide the configuration information of the selected group
  593. */
  594. static void ti_iodelay_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
  595. struct seq_file *s,
  596. unsigned int selector)
  597. {
  598. struct ti_iodelay_device *iod;
  599. struct ti_iodelay_pingroup *group;
  600. int i;
  601. iod = pinctrl_dev_get_drvdata(pctldev);
  602. group = ti_iodelay_get_pingroup(iod, selector);
  603. if (!group)
  604. return;
  605. for (i = 0; i < group->ncfg; i++) {
  606. struct ti_iodelay_cfg *cfg;
  607. u32 reg = 0;
  608. cfg = &group->cfg[i];
  609. regmap_read(iod->regmap, cfg->offset, &reg),
  610. seq_printf(s, "\n\t0x%08x = 0x%08x (%3d, %3d)",
  611. cfg->offset, reg, cfg->a_delay,
  612. cfg->g_delay);
  613. }
  614. }
  615. #endif
  616. static const struct pinctrl_ops ti_iodelay_pinctrl_ops = {
  617. .get_groups_count = pinctrl_generic_get_group_count,
  618. .get_group_name = pinctrl_generic_get_group_name,
  619. .get_group_pins = pinctrl_generic_get_group_pins,
  620. #ifdef CONFIG_DEBUG_FS
  621. .pin_dbg_show = ti_iodelay_pin_dbg_show,
  622. #endif
  623. .dt_node_to_map = ti_iodelay_dt_node_to_map,
  624. };
  625. static const struct pinconf_ops ti_iodelay_pinctrl_pinconf_ops = {
  626. .pin_config_group_get = ti_iodelay_pinconf_group_get,
  627. .pin_config_group_set = ti_iodelay_pinconf_group_set,
  628. #ifdef CONFIG_DEBUG_FS
  629. .pin_config_group_dbg_show = ti_iodelay_pinconf_group_dbg_show,
  630. #endif
  631. };
  632. /**
  633. * ti_iodelay_alloc_pins() - Allocate structures needed for pins for iodelay
  634. * @dev: Device pointer
  635. * @iod: iodelay device
  636. * @base_phy: Base Physical Address
  637. *
  638. * Return: 0 if all went fine, else appropriate error value.
  639. */
  640. static int ti_iodelay_alloc_pins(struct device *dev,
  641. struct ti_iodelay_device *iod, u32 base_phy)
  642. {
  643. const struct ti_iodelay_reg_data *r = iod->reg_data;
  644. struct pinctrl_pin_desc *pin;
  645. u32 phy_reg;
  646. int nr_pins, i;
  647. nr_pins = ti_iodelay_offset_to_pin(iod, r->regmap_config->max_register);
  648. dev_dbg(dev, "Allocating %i pins\n", nr_pins);
  649. iod->pa = devm_kcalloc(dev, nr_pins, sizeof(*iod->pa), GFP_KERNEL);
  650. if (!iod->pa)
  651. return -ENOMEM;
  652. iod->desc.pins = iod->pa;
  653. iod->desc.npins = nr_pins;
  654. phy_reg = r->reg_start_offset + base_phy;
  655. for (i = 0; i < nr_pins; i++, phy_reg += 4) {
  656. pin = &iod->pa[i];
  657. pin->number = i;
  658. }
  659. return 0;
  660. }
  661. static struct regmap_config dra7_iodelay_regmap_config = {
  662. .reg_bits = 32,
  663. .reg_stride = 4,
  664. .val_bits = 32,
  665. .max_register = 0xd1c,
  666. };
  667. static struct ti_iodelay_reg_data dra7_iodelay_data = {
  668. .signature_mask = 0x0003f000,
  669. .signature_value = 0x29,
  670. .lock_mask = 0x00000400,
  671. .lock_val = 1,
  672. .unlock_val = 0,
  673. .binary_data_coarse_mask = 0x000003e0,
  674. .binary_data_fine_mask = 0x0000001f,
  675. .reg_refclk_offset = 0x14,
  676. .refclk_period_mask = 0xffff,
  677. .reg_coarse_offset = 0x18,
  678. .coarse_delay_count_mask = 0xffff0000,
  679. .coarse_ref_count_mask = 0x0000ffff,
  680. .reg_fine_offset = 0x1C,
  681. .fine_delay_count_mask = 0xffff0000,
  682. .fine_ref_count_mask = 0x0000ffff,
  683. .reg_global_lock_offset = 0x2c,
  684. .global_lock_mask = 0x0000ffff,
  685. .global_unlock_val = 0x0000aaaa,
  686. .global_lock_val = 0x0000aaab,
  687. .reg_start_offset = 0x30,
  688. .reg_nr_per_pin = 3,
  689. .regmap_config = &dra7_iodelay_regmap_config,
  690. };
  691. static const struct of_device_id ti_iodelay_of_match[] = {
  692. {.compatible = "ti,dra7-iodelay", .data = &dra7_iodelay_data},
  693. { /* Hopefully no more.. */ },
  694. };
  695. MODULE_DEVICE_TABLE(of, ti_iodelay_of_match);
  696. /**
  697. * ti_iodelay_probe() - Standard probe
  698. * @pdev: platform device
  699. *
  700. * Return: 0 if all went fine, else appropriate error value.
  701. */
  702. static int ti_iodelay_probe(struct platform_device *pdev)
  703. {
  704. struct device *dev = &pdev->dev;
  705. struct device_node *np = of_node_get(dev->of_node);
  706. const struct of_device_id *match;
  707. struct resource *res;
  708. struct ti_iodelay_device *iod;
  709. int ret = 0;
  710. if (!np) {
  711. ret = -EINVAL;
  712. dev_err(dev, "No OF node\n");
  713. goto exit_out;
  714. }
  715. match = of_match_device(ti_iodelay_of_match, dev);
  716. if (!match) {
  717. ret = -EINVAL;
  718. dev_err(dev, "No DATA match\n");
  719. goto exit_out;
  720. }
  721. iod = devm_kzalloc(dev, sizeof(*iod), GFP_KERNEL);
  722. if (!iod) {
  723. ret = -ENOMEM;
  724. goto exit_out;
  725. }
  726. iod->dev = dev;
  727. iod->reg_data = match->data;
  728. /* So far We can assume there is only 1 bank of registers */
  729. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  730. if (!res) {
  731. dev_err(dev, "Missing MEM resource\n");
  732. ret = -ENODEV;
  733. goto exit_out;
  734. }
  735. iod->phys_base = res->start;
  736. iod->reg_base = devm_ioremap_resource(dev, res);
  737. if (IS_ERR(iod->reg_base)) {
  738. ret = PTR_ERR(iod->reg_base);
  739. goto exit_out;
  740. }
  741. iod->regmap = devm_regmap_init_mmio(dev, iod->reg_base,
  742. iod->reg_data->regmap_config);
  743. if (IS_ERR(iod->regmap)) {
  744. dev_err(dev, "Regmap MMIO init failed.\n");
  745. ret = PTR_ERR(iod->regmap);
  746. goto exit_out;
  747. }
  748. if (ti_iodelay_pinconf_init_dev(iod))
  749. goto exit_out;
  750. ret = ti_iodelay_alloc_pins(dev, iod, res->start);
  751. if (ret)
  752. goto exit_out;
  753. iod->desc.pctlops = &ti_iodelay_pinctrl_ops;
  754. /* no pinmux ops - we are pinconf */
  755. iod->desc.confops = &ti_iodelay_pinctrl_pinconf_ops;
  756. iod->desc.name = dev_name(dev);
  757. iod->desc.owner = THIS_MODULE;
  758. ret = pinctrl_register_and_init(&iod->desc, dev, iod, &iod->pctl);
  759. if (ret) {
  760. dev_err(dev, "Failed to register pinctrl\n");
  761. goto exit_out;
  762. }
  763. platform_set_drvdata(pdev, iod);
  764. return pinctrl_enable(iod->pctl);
  765. exit_out:
  766. of_node_put(np);
  767. return ret;
  768. }
  769. /**
  770. * ti_iodelay_remove() - standard remove
  771. * @pdev: platform device
  772. *
  773. * Return: 0 if all went fine, else appropriate error value.
  774. */
  775. static int ti_iodelay_remove(struct platform_device *pdev)
  776. {
  777. struct ti_iodelay_device *iod = platform_get_drvdata(pdev);
  778. if (!iod)
  779. return 0;
  780. if (iod->pctl)
  781. pinctrl_unregister(iod->pctl);
  782. ti_iodelay_pinconf_deinit_dev(iod);
  783. /* Expect other allocations to be freed by devm */
  784. return 0;
  785. }
  786. static struct platform_driver ti_iodelay_driver = {
  787. .probe = ti_iodelay_probe,
  788. .remove = ti_iodelay_remove,
  789. .driver = {
  790. .owner = THIS_MODULE,
  791. .name = DRIVER_NAME,
  792. .of_match_table = ti_iodelay_of_match,
  793. },
  794. };
  795. module_platform_driver(ti_iodelay_driver);
  796. MODULE_AUTHOR("Texas Instruments, Inc.");
  797. MODULE_DESCRIPTION("Pinconf driver for TI's IO Delay module");
  798. MODULE_LICENSE("GPL v2");