treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / mtd / hyperbus / hbmc-am654.c
blob08d543b124cd14e5cc250fff490a5a5c84048a92
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Copyright (C) 2019 Texas Instruments Incorporated - http://www.ti.com/
4 // Author: Vignesh Raghavendra <vigneshr@ti.com>
6 #include <linux/err.h>
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/mtd/cfi.h>
10 #include <linux/mtd/hyperbus.h>
11 #include <linux/mtd/mtd.h>
12 #include <linux/mux/consumer.h>
13 #include <linux/of.h>
14 #include <linux/platform_device.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/types.h>
18 #define AM654_HBMC_CALIB_COUNT 25
20 struct am654_hbmc_priv {
21 struct hyperbus_ctlr ctlr;
22 struct hyperbus_device hbdev;
23 struct mux_control *mux_ctrl;
26 static int am654_hbmc_calibrate(struct hyperbus_device *hbdev)
28 struct map_info *map = &hbdev->map;
29 struct cfi_private cfi;
30 int count = AM654_HBMC_CALIB_COUNT;
31 int pass_count = 0;
32 int ret;
34 cfi.interleave = 1;
35 cfi.device_type = CFI_DEVICETYPE_X16;
36 cfi_send_gen_cmd(0xF0, 0, 0, map, &cfi, cfi.device_type, NULL);
37 cfi_send_gen_cmd(0x98, 0x55, 0, map, &cfi, cfi.device_type, NULL);
39 while (count--) {
40 ret = cfi_qry_present(map, 0, &cfi);
41 if (ret)
42 pass_count++;
43 else
44 pass_count = 0;
45 if (pass_count == 5)
46 break;
49 cfi_qry_mode_off(0, map, &cfi);
51 return ret;
54 static const struct hyperbus_ops am654_hbmc_ops = {
55 .calibrate = am654_hbmc_calibrate,
58 static int am654_hbmc_probe(struct platform_device *pdev)
60 struct device *dev = &pdev->dev;
61 struct am654_hbmc_priv *priv;
62 int ret;
64 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
65 if (!priv)
66 return -ENOMEM;
68 platform_set_drvdata(pdev, priv);
70 if (of_property_read_bool(dev->of_node, "mux-controls")) {
71 struct mux_control *control = devm_mux_control_get(dev, NULL);
73 if (IS_ERR(control))
74 return PTR_ERR(control);
76 ret = mux_control_select(control, 1);
77 if (ret) {
78 dev_err(dev, "Failed to select HBMC mux\n");
79 return ret;
81 priv->mux_ctrl = control;
84 pm_runtime_enable(dev);
85 ret = pm_runtime_get_sync(dev);
86 if (ret < 0) {
87 pm_runtime_put_noidle(dev);
88 goto disable_pm;
91 priv->ctlr.dev = dev;
92 priv->ctlr.ops = &am654_hbmc_ops;
93 priv->hbdev.ctlr = &priv->ctlr;
94 priv->hbdev.np = of_get_next_child(dev->of_node, NULL);
95 ret = hyperbus_register_device(&priv->hbdev);
96 if (ret) {
97 dev_err(dev, "failed to register controller\n");
98 pm_runtime_put_sync(&pdev->dev);
99 goto disable_pm;
102 return 0;
103 disable_pm:
104 pm_runtime_disable(dev);
105 if (priv->mux_ctrl)
106 mux_control_deselect(priv->mux_ctrl);
107 return ret;
110 static int am654_hbmc_remove(struct platform_device *pdev)
112 struct am654_hbmc_priv *priv = platform_get_drvdata(pdev);
113 int ret;
115 ret = hyperbus_unregister_device(&priv->hbdev);
116 if (priv->mux_ctrl)
117 mux_control_deselect(priv->mux_ctrl);
118 pm_runtime_put_sync(&pdev->dev);
119 pm_runtime_disable(&pdev->dev);
121 return ret;
124 static const struct of_device_id am654_hbmc_dt_ids[] = {
126 .compatible = "ti,am654-hbmc",
128 { /* end of table */ }
131 MODULE_DEVICE_TABLE(of, am654_hbmc_dt_ids);
133 static struct platform_driver am654_hbmc_platform_driver = {
134 .probe = am654_hbmc_probe,
135 .remove = am654_hbmc_remove,
136 .driver = {
137 .name = "hbmc-am654",
138 .of_match_table = am654_hbmc_dt_ids,
142 module_platform_driver(am654_hbmc_platform_driver);
144 MODULE_DESCRIPTION("HBMC driver for AM654 SoC");
145 MODULE_LICENSE("GPL v2");
146 MODULE_ALIAS("platform:hbmc-am654");
147 MODULE_AUTHOR("Vignesh Raghavendra <vigneshr@ti.com>");