summaryrefstree
path: root/kernel/techpack/audio/asoc/codecs/rouleur/pm2250_spmi.c
blob: 10a7d109bbbe51f4e4f62a9fc74289db3f8e35c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (c) 2020, The Linux Foundation. All rights reserved.
 */

#include <linux/of.h>
#include <linux/of_platform.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/pm.h>
#include <linux/regmap.h>
#include <linux/slab.h>

/**
 * @regmap: regmap used to access PMIC registers
 */
struct pm2250_spmi {
	struct regmap *regmap;
};

static const struct of_device_id pm2250_id_table[] = {
	{ .compatible = "qcom,pm2250-spmi" },
	{ },
};
MODULE_DEVICE_TABLE(of, pm2250_id_table);

/**
 * pm2250_spmi_write: Function to write to PMIC register
 * @device: node for rouleur device
 * @reg: PMIC register to write value
 * @value: Value to be written to PMIC register
 */
int pm2250_spmi_write(struct device *dev, int reg, int value)
{
	int rc;
	struct pm2250_spmi *spmi_dd;

	if (!of_device_is_compatible(dev->of_node, "qcom,pm2250-spmi")) {
		pr_err("%s: Device node is invalid\n", __func__);
		return -EINVAL;
	}

	spmi_dd = dev_get_drvdata(dev);
	if (!spmi_dd)
		return -EINVAL;

	rc = regmap_write(spmi_dd->regmap, reg, value);
	if (rc)
		dev_err(dev, "%s: Write to PMIC register failed\n", __func__);

	return rc;
}
EXPORT_SYMBOL(pm2250_spmi_write);

/**
 * pm2250_spmi_read: Function to read PMIC register
 * @device: node for rouleur device
 * @reg: PMIC register to read value
 * @value: Pointer to value of reg to be read
 */
int pm2250_spmi_read(struct device *dev, int reg, int *value)
{
	int rc;
	struct pm2250_spmi *spmi_dd;

	if (!of_device_is_compatible(dev->of_node, "qcom,pm2250-spmi")) {
		pr_err("%s: Device node is invalid\n", __func__);
		return -EINVAL;
	}

	spmi_dd = dev_get_drvdata(dev);
	if (!spmi_dd)
		return -EINVAL;

	rc = regmap_read(spmi_dd->regmap, reg, value);
	if (rc)
		dev_err(dev, "%s: Read from PMIC register failed\n", __func__);

	return rc;
}
EXPORT_SYMBOL(pm2250_spmi_read);

static int pm2250_spmi_probe(struct platform_device *pdev)
{
	struct pm2250_spmi *spmi_dd;
	const struct of_device_id *match;

	match = of_match_node(pm2250_id_table, pdev->dev.of_node);
	if (!match)
		return -ENXIO;

	spmi_dd = devm_kzalloc(&pdev->dev, sizeof(*spmi_dd), GFP_KERNEL);
	if (spmi_dd == NULL)
		return -ENOMEM;

	spmi_dd->regmap = dev_get_regmap(pdev->dev.parent, NULL);
	if (!spmi_dd->regmap) {
		dev_err(&pdev->dev, "Parent regmap unavailable.\n");
		return -ENXIO;
	}

	platform_set_drvdata(pdev, spmi_dd);

	dev_dbg(&pdev->dev, "Probe success !!\n");

	return 0;
}

static int pm2250_spmi_remove(struct platform_device *pdev)
{
	of_platform_depopulate(&pdev->dev);
	return 0;
}

static struct platform_driver pm2250_spmi_driver = {
	.probe		= pm2250_spmi_probe,
	.remove		= pm2250_spmi_remove,
	.driver	= {
		.name		= "pm2250-spmi",
		.of_match_table	= pm2250_id_table,
	},
};
module_platform_driver(pm2250_spmi_driver);

MODULE_ALIAS("platform:pm2250-spmi");
MODULE_DESCRIPTION("PMIC SPMI driver");
MODULE_LICENSE("GPL v2");