2 * PowerNV OPAL Power-Shift-Ratio interface
4 * Copyright 2017 IBM Corp.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #define pr_fmt(fmt) "opal-psr: " fmt
15 #include <linux/kobject.h>
16 #include <linux/slab.h>
20 DEFINE_MUTEX(psr_mutex
);
22 static struct kobject
*psr_kobj
;
26 struct kobj_attribute attr
;
29 static ssize_t
psr_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
32 struct psr_attr
*psr_attr
= container_of(attr
, struct psr_attr
, attr
);
36 token
= opal_async_get_token_interruptible();
38 pr_devel("Failed to get token\n");
42 ret
= mutex_lock_interruptible(&psr_mutex
);
46 ret
= opal_get_power_shift_ratio(psr_attr
->handle
, token
,
49 case OPAL_ASYNC_COMPLETION
:
50 ret
= opal_async_wait_response(token
, &msg
);
52 pr_devel("Failed to wait for the async response\n");
56 ret
= opal_error_code(opal_get_async_rc(msg
));
58 ret
= sprintf(buf
, "%u\n", be32_to_cpu(psr
));
64 ret
= sprintf(buf
, "%u\n", be32_to_cpu(psr
));
69 ret
= opal_error_code(ret
);
73 mutex_unlock(&psr_mutex
);
75 opal_async_release_token(token
);
79 static ssize_t
psr_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
80 const char *buf
, size_t count
)
82 struct psr_attr
*psr_attr
= container_of(attr
, struct psr_attr
, attr
);
86 ret
= kstrtoint(buf
, 0, &psr
);
90 token
= opal_async_get_token_interruptible();
92 pr_devel("Failed to get token\n");
96 ret
= mutex_lock_interruptible(&psr_mutex
);
100 ret
= opal_set_power_shift_ratio(psr_attr
->handle
, token
, psr
);
102 case OPAL_ASYNC_COMPLETION
:
103 ret
= opal_async_wait_response(token
, &msg
);
105 pr_devel("Failed to wait for the async response\n");
109 ret
= opal_error_code(opal_get_async_rc(msg
));
117 ret
= opal_error_code(ret
);
121 mutex_unlock(&psr_mutex
);
123 opal_async_release_token(token
);
127 void __init
opal_psr_init(void)
129 struct device_node
*psr
, *node
;
132 psr
= of_find_compatible_node(NULL
, NULL
,
133 "ibm,opal-power-shift-ratio");
135 pr_devel("Power-shift-ratio node not found\n");
139 psr_attrs
= kcalloc(of_get_child_count(psr
), sizeof(struct psr_attr
),
144 psr_kobj
= kobject_create_and_add("psr", opal_kobj
);
146 pr_warn("Failed to create psr kobject\n");
150 for_each_child_of_node(psr
, node
) {
151 if (of_property_read_u32(node
, "handle",
152 &psr_attrs
[i
].handle
))
155 sysfs_attr_init(&psr_attrs
[i
].attr
.attr
);
156 if (of_property_read_string(node
, "label",
157 &psr_attrs
[i
].attr
.attr
.name
))
159 psr_attrs
[i
].attr
.attr
.mode
= 0664;
160 psr_attrs
[i
].attr
.show
= psr_show
;
161 psr_attrs
[i
].attr
.store
= psr_store
;
162 if (sysfs_create_file(psr_kobj
, &psr_attrs
[i
].attr
.attr
)) {
163 pr_devel("Failed to create psr sysfs file %s\n",
164 psr_attrs
[i
].attr
.attr
.name
);
172 kobject_put(psr_kobj
);