Banajit Goswami | de8271c | 2017-01-18 00:28:59 -0800 | [diff] [blame^] | 1 | /* Copyright (c) 2016, The Linux Foundation. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/of.h> |
| 18 | #include <linux/of_device.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/gpio.h> |
| 21 | #include <linux/of_gpio.h> |
| 22 | #include <linux/mfd/msm-cdc-pinctrl.h> |
| 23 | |
| 24 | struct msm_cdc_pinctrl_info { |
| 25 | struct pinctrl *pinctrl; |
| 26 | struct pinctrl_state *pinctrl_active; |
| 27 | struct pinctrl_state *pinctrl_sleep; |
| 28 | int gpio; |
| 29 | bool state; |
| 30 | }; |
| 31 | |
| 32 | static struct msm_cdc_pinctrl_info *msm_cdc_pinctrl_get_gpiodata( |
| 33 | struct device_node *np) |
| 34 | { |
| 35 | struct platform_device *pdev; |
| 36 | struct msm_cdc_pinctrl_info *gpio_data; |
| 37 | |
| 38 | if (!np) { |
| 39 | pr_err("%s: device node is null\n", __func__); |
| 40 | return NULL; |
| 41 | } |
| 42 | |
| 43 | pdev = of_find_device_by_node(np); |
| 44 | if (!pdev) { |
| 45 | pr_err("%s: platform device not found!\n", __func__); |
| 46 | return NULL; |
| 47 | } |
| 48 | |
| 49 | gpio_data = dev_get_drvdata(&pdev->dev); |
| 50 | if (!gpio_data) |
| 51 | dev_err(&pdev->dev, "%s: cannot find cdc gpio info\n", |
| 52 | __func__); |
| 53 | |
| 54 | return gpio_data; |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * msm_cdc_get_gpio_state: select pinctrl sleep state |
| 59 | * @np: pointer to struct device_node |
| 60 | * |
| 61 | * Returns error code for failure and GPIO value on success |
| 62 | */ |
| 63 | int msm_cdc_get_gpio_state(struct device_node *np) |
| 64 | { |
| 65 | struct msm_cdc_pinctrl_info *gpio_data; |
| 66 | int value = -EINVAL; |
| 67 | |
| 68 | gpio_data = msm_cdc_pinctrl_get_gpiodata(np); |
| 69 | if (!gpio_data) |
| 70 | return value; |
| 71 | |
| 72 | if (gpio_is_valid(gpio_data->gpio)) |
| 73 | value = gpio_get_value_cansleep(gpio_data->gpio); |
| 74 | |
| 75 | return value; |
| 76 | } |
| 77 | EXPORT_SYMBOL(msm_cdc_get_gpio_state); |
| 78 | |
| 79 | /* |
| 80 | * msm_cdc_pinctrl_select_sleep_state: select pinctrl sleep state |
| 81 | * @np: pointer to struct device_node |
| 82 | * |
| 83 | * Returns error code for failure |
| 84 | */ |
| 85 | int msm_cdc_pinctrl_select_sleep_state(struct device_node *np) |
| 86 | { |
| 87 | struct msm_cdc_pinctrl_info *gpio_data; |
| 88 | |
| 89 | gpio_data = msm_cdc_pinctrl_get_gpiodata(np); |
| 90 | if (!gpio_data) |
| 91 | return -EINVAL; |
| 92 | |
| 93 | if (!gpio_data->pinctrl_sleep) { |
| 94 | pr_err("%s: pinctrl sleep state is null\n", __func__); |
| 95 | return -EINVAL; |
| 96 | } |
| 97 | gpio_data->state = false; |
| 98 | |
| 99 | return pinctrl_select_state(gpio_data->pinctrl, |
| 100 | gpio_data->pinctrl_sleep); |
| 101 | } |
| 102 | EXPORT_SYMBOL(msm_cdc_pinctrl_select_sleep_state); |
| 103 | |
| 104 | /* |
| 105 | * msm_cdc_pinctrl_select_active_state: select pinctrl active state |
| 106 | * @np: pointer to struct device_node |
| 107 | * |
| 108 | * Returns error code for failure |
| 109 | */ |
| 110 | int msm_cdc_pinctrl_select_active_state(struct device_node *np) |
| 111 | { |
| 112 | struct msm_cdc_pinctrl_info *gpio_data; |
| 113 | |
| 114 | gpio_data = msm_cdc_pinctrl_get_gpiodata(np); |
| 115 | if (!gpio_data) |
| 116 | return -EINVAL; |
| 117 | |
| 118 | if (!gpio_data->pinctrl_active) { |
| 119 | pr_err("%s: pinctrl active state is null\n", __func__); |
| 120 | return -EINVAL; |
| 121 | } |
| 122 | gpio_data->state = true; |
| 123 | |
| 124 | return pinctrl_select_state(gpio_data->pinctrl, |
| 125 | gpio_data->pinctrl_active); |
| 126 | } |
| 127 | EXPORT_SYMBOL(msm_cdc_pinctrl_select_active_state); |
| 128 | |
| 129 | /* |
| 130 | * msm_cdc_pinctrl_get_state: get curren pinctrl state |
| 131 | * @np: pointer to struct device_node |
| 132 | * |
| 133 | * Returns 0 for sleep state, 1 for active state |
| 134 | */ |
| 135 | bool msm_cdc_pinctrl_get_state(struct device_node *np) |
| 136 | { |
| 137 | struct msm_cdc_pinctrl_info *gpio_data; |
| 138 | |
| 139 | gpio_data = msm_cdc_pinctrl_get_gpiodata(np); |
| 140 | if (!gpio_data) |
| 141 | return -EINVAL; |
| 142 | |
| 143 | return gpio_data->state; |
| 144 | } |
| 145 | EXPORT_SYMBOL(msm_cdc_pinctrl_get_state); |
| 146 | |
| 147 | static int msm_cdc_pinctrl_probe(struct platform_device *pdev) |
| 148 | { |
| 149 | int ret = 0; |
| 150 | struct msm_cdc_pinctrl_info *gpio_data; |
| 151 | |
| 152 | gpio_data = devm_kzalloc(&pdev->dev, |
| 153 | sizeof(struct msm_cdc_pinctrl_info), |
| 154 | GFP_KERNEL); |
| 155 | if (!gpio_data) |
| 156 | return -ENOMEM; |
| 157 | |
| 158 | gpio_data->pinctrl = devm_pinctrl_get(&pdev->dev); |
| 159 | if (IS_ERR_OR_NULL(gpio_data->pinctrl)) { |
| 160 | dev_err(&pdev->dev, "%s: Cannot get cdc gpio pinctrl:%ld\n", |
| 161 | __func__, PTR_ERR(gpio_data->pinctrl)); |
| 162 | ret = PTR_ERR(gpio_data->pinctrl); |
| 163 | goto err_pctrl_get; |
| 164 | } |
| 165 | |
| 166 | gpio_data->pinctrl_active = pinctrl_lookup_state( |
| 167 | gpio_data->pinctrl, "aud_active"); |
| 168 | if (IS_ERR_OR_NULL(gpio_data->pinctrl_active)) { |
| 169 | dev_err(&pdev->dev, "%s: Cannot get aud_active pinctrl state:%ld\n", |
| 170 | __func__, PTR_ERR(gpio_data->pinctrl_active)); |
| 171 | ret = PTR_ERR(gpio_data->pinctrl_active); |
| 172 | goto err_lookup_state; |
| 173 | } |
| 174 | |
| 175 | gpio_data->pinctrl_sleep = pinctrl_lookup_state( |
| 176 | gpio_data->pinctrl, "aud_sleep"); |
| 177 | if (IS_ERR_OR_NULL(gpio_data->pinctrl_sleep)) { |
| 178 | dev_err(&pdev->dev, "%s: Cannot get aud_sleep pinctrl state:%ld\n", |
| 179 | __func__, PTR_ERR(gpio_data->pinctrl_sleep)); |
| 180 | ret = PTR_ERR(gpio_data->pinctrl_sleep); |
| 181 | goto err_lookup_state; |
| 182 | } |
| 183 | |
| 184 | /* Set pinctrl state to aud_sleep by default */ |
| 185 | ret = pinctrl_select_state(gpio_data->pinctrl, |
| 186 | gpio_data->pinctrl_sleep); |
| 187 | if (ret) |
| 188 | dev_err(&pdev->dev, "%s: set cdc gpio sleep state fail: %d\n", |
| 189 | __func__, ret); |
| 190 | |
| 191 | gpio_data->gpio = of_get_named_gpio(pdev->dev.of_node, |
| 192 | "qcom,cdc-rst-n-gpio", 0); |
| 193 | if (gpio_is_valid(gpio_data->gpio)) { |
| 194 | ret = gpio_request(gpio_data->gpio, "MSM_CDC_RESET"); |
| 195 | if (ret) { |
| 196 | dev_err(&pdev->dev, "%s: Failed to request gpio %d\n", |
| 197 | __func__, gpio_data->gpio); |
| 198 | goto err_lookup_state; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | dev_set_drvdata(&pdev->dev, gpio_data); |
| 203 | return 0; |
| 204 | |
| 205 | err_lookup_state: |
| 206 | devm_pinctrl_put(gpio_data->pinctrl); |
| 207 | err_pctrl_get: |
| 208 | devm_kfree(&pdev->dev, gpio_data); |
| 209 | return ret; |
| 210 | } |
| 211 | |
| 212 | static int msm_cdc_pinctrl_remove(struct platform_device *pdev) |
| 213 | { |
| 214 | struct msm_cdc_pinctrl_info *gpio_data; |
| 215 | |
| 216 | gpio_data = dev_get_drvdata(&pdev->dev); |
| 217 | |
| 218 | if (gpio_data && gpio_data->pinctrl) |
| 219 | devm_pinctrl_put(gpio_data->pinctrl); |
| 220 | |
| 221 | devm_kfree(&pdev->dev, gpio_data); |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | static const struct of_device_id msm_cdc_pinctrl_match[] = { |
| 227 | {.compatible = "qcom,msm-cdc-pinctrl"}, |
| 228 | {} |
| 229 | }; |
| 230 | |
| 231 | static struct platform_driver msm_cdc_pinctrl_driver = { |
| 232 | .driver = { |
| 233 | .name = "msm-cdc-pinctrl", |
| 234 | .owner = THIS_MODULE, |
| 235 | .of_match_table = msm_cdc_pinctrl_match, |
| 236 | }, |
| 237 | .probe = msm_cdc_pinctrl_probe, |
| 238 | .remove = msm_cdc_pinctrl_remove, |
| 239 | }; |
| 240 | module_platform_driver(msm_cdc_pinctrl_driver); |
| 241 | |
| 242 | MODULE_DESCRIPTION("MSM CODEC pin control platform driver"); |
| 243 | MODULE_LICENSE("GPL v2"); |